1cc528b9dSMatthias Ringwald /* 2cc528b9dSMatthias Ringwald * Copyright (C) 2023 BlueKitchen GmbH 3cc528b9dSMatthias Ringwald * 4cc528b9dSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5cc528b9dSMatthias Ringwald * modification, are permitted provided that the following conditions 6cc528b9dSMatthias Ringwald * are met: 7cc528b9dSMatthias Ringwald * 8cc528b9dSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9cc528b9dSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10cc528b9dSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11cc528b9dSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12cc528b9dSMatthias Ringwald * documentation and/or other materials provided with the distribution. 13cc528b9dSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14cc528b9dSMatthias Ringwald * contributors may be used to endorse or promote products derived 15cc528b9dSMatthias Ringwald * from this software without specific prior written permission. 16cc528b9dSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17cc528b9dSMatthias Ringwald * personal benefit and not for any commercial purpose or for 18cc528b9dSMatthias Ringwald * monetary gain. 19cc528b9dSMatthias Ringwald * 20cc528b9dSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21cc528b9dSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22cc528b9dSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23cc528b9dSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24cc528b9dSMatthias Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25cc528b9dSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26cc528b9dSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27cc528b9dSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28cc528b9dSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29cc528b9dSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30cc528b9dSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31cc528b9dSMatthias Ringwald * SUCH DAMAGE. 32cc528b9dSMatthias Ringwald * 33cc528b9dSMatthias Ringwald * Please inquire about commercial licensing options at 34cc528b9dSMatthias Ringwald * [email protected] 35cc528b9dSMatthias Ringwald * 36cc528b9dSMatthias Ringwald */ 37cc528b9dSMatthias Ringwald 38cc528b9dSMatthias Ringwald #define BTSTACK_FILE__ "main.c" 39cc528b9dSMatthias Ringwald 40cc528b9dSMatthias Ringwald // ***************************************************************************** 41cc528b9dSMatthias Ringwald // 42cc528b9dSMatthias Ringwald // minimal setup for HCI code 43cc528b9dSMatthias Ringwald // 44cc528b9dSMatthias Ringwald // ***************************************************************************** 45cc528b9dSMatthias Ringwald 46cc528b9dSMatthias Ringwald #include <stdint.h> 47cc528b9dSMatthias Ringwald #include <stdio.h> 48cc528b9dSMatthias Ringwald #include <stdlib.h> 49cc528b9dSMatthias Ringwald #include <string.h> 50cc528b9dSMatthias Ringwald #include <signal.h> 51cc528b9dSMatthias Ringwald 52cc528b9dSMatthias Ringwald #include "btstack_config.h" 53cc528b9dSMatthias Ringwald 54cc528b9dSMatthias Ringwald #include "ble/le_device_db_tlv.h" 55cc528b9dSMatthias Ringwald #include "btstack_chipset_nxp.h" 56cc528b9dSMatthias Ringwald #include "btstack_debug.h" 57cc528b9dSMatthias Ringwald #include "btstack_event.h" 58cc528b9dSMatthias Ringwald #include "btstack_memory.h" 59cc528b9dSMatthias Ringwald #include "btstack_run_loop_posix.h" 60cc528b9dSMatthias Ringwald #include "btstack_signal.h" 61cc528b9dSMatthias Ringwald #include "btstack_stdin.h" 62cc528b9dSMatthias Ringwald #include "btstack_tlv_posix.h" 63cc528b9dSMatthias Ringwald #include "btstack_uart.h" 64cc528b9dSMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h" 65cc528b9dSMatthias Ringwald #include "hci.h" 66cc528b9dSMatthias Ringwald #include "hci_dump.h" 67cc528b9dSMatthias Ringwald #include "hci_dump_posix_fs.h" 68cc528b9dSMatthias Ringwald #include "hci_transport_h4.h" 69cc528b9dSMatthias Ringwald 70cc528b9dSMatthias Ringwald 71cc528b9dSMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_" 72cc528b9dSMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv" 73cc528b9dSMatthias Ringwald static char tlv_db_path[100]; 74cc528b9dSMatthias Ringwald static const btstack_tlv_t * tlv_impl; 75cc528b9dSMatthias Ringwald static btstack_tlv_posix_t tlv_context; 76cc528b9dSMatthias Ringwald static bd_addr_t local_addr; 77cc528b9dSMatthias Ringwald 78cc528b9dSMatthias Ringwald static int main_argc; 79cc528b9dSMatthias Ringwald static const char ** main_argv; 80cc528b9dSMatthias Ringwald 81cc528b9dSMatthias Ringwald static const btstack_uart_t * uart_driver; 82cc528b9dSMatthias Ringwald static btstack_uart_config_t uart_config; 83cc528b9dSMatthias Ringwald 84cc528b9dSMatthias Ringwald // shutdown 85cc528b9dSMatthias Ringwald static bool shutdown_triggered; 86cc528b9dSMatthias Ringwald 87cc528b9dSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 88cc528b9dSMatthias Ringwald static void local_version_information_handler(uint8_t * packet); 89cc528b9dSMatthias Ringwald 90cc528b9dSMatthias Ringwald static hci_transport_config_uart_t transport_config = { 91cc528b9dSMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 92cc528b9dSMatthias Ringwald 115200, 93cc528b9dSMatthias Ringwald 0, // main baudrate 942364ad0cSMatthias Ringwald 1, // flow control 95cc528b9dSMatthias Ringwald NULL, 96cc528b9dSMatthias Ringwald }; 97cc528b9dSMatthias Ringwald 98cc528b9dSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 99cc528b9dSMatthias Ringwald 100cc528b9dSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 101cc528b9dSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 102cc528b9dSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 103cc528b9dSMatthias Ringwald case BTSTACK_EVENT_STATE: 104cc528b9dSMatthias Ringwald switch(btstack_event_state_get_state(packet)){ 105cc528b9dSMatthias Ringwald case HCI_STATE_WORKING: 106cc528b9dSMatthias Ringwald gap_local_bd_addr(local_addr); 107cc528b9dSMatthias Ringwald printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 108cc528b9dSMatthias Ringwald btstack_strcpy(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_PREFIX); 109cc528b9dSMatthias Ringwald btstack_strcat(tlv_db_path, sizeof(tlv_db_path), bd_addr_to_str_with_delimiter(local_addr, '-')); 110cc528b9dSMatthias Ringwald btstack_strcat(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_POSTFIX); 111cc528b9dSMatthias Ringwald tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 112cc528b9dSMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context); 113cc528b9dSMatthias Ringwald #ifdef ENABLE_CLASSIC 114cc528b9dSMatthias Ringwald hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); 115cc528b9dSMatthias Ringwald #endif 116cc528b9dSMatthias Ringwald #ifdef ENABLE_BLE 117cc528b9dSMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context); 118cc528b9dSMatthias Ringwald #endif 119cc528b9dSMatthias Ringwald break; 120cc528b9dSMatthias Ringwald case HCI_STATE_OFF: 121cc528b9dSMatthias Ringwald btstack_tlv_posix_deinit(&tlv_context); 122cc528b9dSMatthias Ringwald if (!shutdown_triggered) break; 123cc528b9dSMatthias Ringwald // reset stdin 124cc528b9dSMatthias Ringwald btstack_stdin_reset(); 125cc528b9dSMatthias Ringwald log_info("Good bye, see you.\n"); 126cc528b9dSMatthias Ringwald exit(0); 127cc528b9dSMatthias Ringwald break; 128cc528b9dSMatthias Ringwald default: 129cc528b9dSMatthias Ringwald break; 130cc528b9dSMatthias Ringwald } 131cc528b9dSMatthias Ringwald break; 132cc528b9dSMatthias Ringwald default: 133cc528b9dSMatthias Ringwald break; 134cc528b9dSMatthias Ringwald } 135cc528b9dSMatthias Ringwald } 136cc528b9dSMatthias Ringwald 137cc528b9dSMatthias Ringwald static void trigger_shutdown(void){ 138cc528b9dSMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 139cc528b9dSMatthias Ringwald log_info("sigint_handler: shutting down"); 140cc528b9dSMatthias Ringwald shutdown_triggered = true; 141cc528b9dSMatthias Ringwald hci_power_control(HCI_POWER_OFF); 142cc528b9dSMatthias Ringwald } 143cc528b9dSMatthias Ringwald 144cc528b9dSMatthias Ringwald static int led_state = 0; 145cc528b9dSMatthias Ringwald void hal_led_toggle(void){ 146cc528b9dSMatthias Ringwald led_state = 1 - led_state; 147cc528b9dSMatthias Ringwald printf("LED State %u\n", led_state); 148cc528b9dSMatthias Ringwald } 149cc528b9dSMatthias Ringwald 150cc528b9dSMatthias Ringwald static void nxp_phase2(uint8_t status){ 151cc528b9dSMatthias Ringwald 152cc528b9dSMatthias Ringwald if (status){ 153cc528b9dSMatthias Ringwald printf("Download firmware failed\n"); 154cc528b9dSMatthias Ringwald return; 155cc528b9dSMatthias Ringwald } 156cc528b9dSMatthias Ringwald 157cc528b9dSMatthias Ringwald printf("Phase 2: Main app\n"); 158cc528b9dSMatthias Ringwald 159*c6e26c7dSMatthias Ringwald // re-configure UART: enable flow control, set initial baudrate 160*c6e26c7dSMatthias Ringwald transport_config.baudrate_init = btstack_chipset_nxp_get_initial_baudrate(); 1612364ad0cSMatthias Ringwald uart_driver->set_flowcontrol(transport_config.flowcontrol); 1622364ad0cSMatthias Ringwald 163cc528b9dSMatthias Ringwald // init HCI 164cc528b9dSMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance_for_uart(uart_driver); 165cc528b9dSMatthias Ringwald hci_init(transport, (void*) &transport_config); 1669d1825b2SMatthias Ringwald hci_set_chipset(btstack_chipset_nxp_instance()); 167cc528b9dSMatthias Ringwald 168cc528b9dSMatthias Ringwald // inform about BTstack state 169cc528b9dSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 170cc528b9dSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 171cc528b9dSMatthias Ringwald 172cc528b9dSMatthias Ringwald // register callback for CTRL-c 173cc528b9dSMatthias Ringwald btstack_signal_register_callback(SIGINT, &trigger_shutdown); 174cc528b9dSMatthias Ringwald 175cc528b9dSMatthias Ringwald // setup app 176cc528b9dSMatthias Ringwald btstack_main(main_argc, main_argv); 177cc528b9dSMatthias Ringwald } 178cc528b9dSMatthias Ringwald 179cc528b9dSMatthias Ringwald int main(int argc, const char * argv[]){ 180cc528b9dSMatthias Ringwald 181cc528b9dSMatthias Ringwald /// GET STARTED with BTstack /// 182cc528b9dSMatthias Ringwald btstack_memory_init(); 183cc528b9dSMatthias Ringwald btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 184cc528b9dSMatthias Ringwald 185cc528b9dSMatthias Ringwald // log into file using HCI_DUMP_PACKETLOGGER format 186cc528b9dSMatthias Ringwald const char * pklg_path = "/tmp/hci_dump.pklg"; 187cc528b9dSMatthias Ringwald hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER); 188cc528b9dSMatthias Ringwald const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance(); 189cc528b9dSMatthias Ringwald hci_dump_init(hci_dump_impl); 190cc528b9dSMatthias Ringwald printf("Packet Log: %s\n", pklg_path); 191cc528b9dSMatthias Ringwald 192cc528b9dSMatthias Ringwald // pick serial port 193cc528b9dSMatthias Ringwald transport_config.device_name = "/dev/tty.usbserial-A506WORJ"; // DVK-ST60-2230C / 88W8997 194cc528b9dSMatthias Ringwald // transport_config.device_name = "/dev/tty.usbserial-FT1XBGIM"; // murata m.2 adapter 195cc528b9dSMatthias Ringwald 196cc528b9dSMatthias Ringwald // accept path from command line 197cc528b9dSMatthias Ringwald if (argc >= 3 && strcmp(argv[1], "-u") == 0){ 198cc528b9dSMatthias Ringwald transport_config.device_name = argv[2]; 199cc528b9dSMatthias Ringwald argc -= 2; 200cc528b9dSMatthias Ringwald memmove((void *) &argv[1], &argv[3], (argc-1) * sizeof(char *)); 201cc528b9dSMatthias Ringwald } 202cc528b9dSMatthias Ringwald printf("H4 device: %s\n", transport_config.device_name); 203cc528b9dSMatthias Ringwald 204cc528b9dSMatthias Ringwald uart_driver = btstack_uart_posix_instance(); 205cc528b9dSMatthias Ringwald 206cc528b9dSMatthias Ringwald // extract UART config from transport config 207cc528b9dSMatthias Ringwald uart_config.baudrate = transport_config.baudrate_init; 2082364ad0cSMatthias Ringwald uart_config.flowcontrol = 0; 209cc528b9dSMatthias Ringwald uart_config.device_name = transport_config.device_name; 210cc528b9dSMatthias Ringwald uart_driver->init(&uart_config); 211cc528b9dSMatthias Ringwald 212cc528b9dSMatthias Ringwald main_argc = argc; 213cc528b9dSMatthias Ringwald main_argv = argv; 214cc528b9dSMatthias Ringwald 215cc528b9dSMatthias Ringwald const char * firmware_v1 = "uartuart8997_bt_v4.bin"; 216*c6e26c7dSMatthias Ringwald printf("Phase 1: Download firmware\n"); 217cc528b9dSMatthias Ringwald btstack_chipset_nxp_set_v1_firmware_path(firmware_v1); 218cc528b9dSMatthias Ringwald btstack_chipset_nxp_download_firmware_with_uart(uart_driver, &nxp_phase2); 219cc528b9dSMatthias Ringwald 220cc528b9dSMatthias Ringwald // go 221cc528b9dSMatthias Ringwald btstack_run_loop_execute(); 222cc528b9dSMatthias Ringwald return 0; 223cc528b9dSMatthias Ringwald } 224