18caefee3SMatthias Ringwald /* 28caefee3SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 38caefee3SMatthias Ringwald * 48caefee3SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 58caefee3SMatthias Ringwald * modification, are permitted provided that the following conditions 68caefee3SMatthias Ringwald * are met: 78caefee3SMatthias Ringwald * 88caefee3SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 98caefee3SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 108caefee3SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 118caefee3SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 128caefee3SMatthias Ringwald * documentation and/or other materials provided with the distribution. 138caefee3SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 148caefee3SMatthias Ringwald * contributors may be used to endorse or promote products derived 158caefee3SMatthias Ringwald * from this software without specific prior written permission. 168caefee3SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 178caefee3SMatthias Ringwald * personal benefit and not for any commercial purpose or for 188caefee3SMatthias Ringwald * monetary gain. 198caefee3SMatthias Ringwald * 208caefee3SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 218caefee3SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 228caefee3SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 238caefee3SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 248caefee3SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 258caefee3SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 268caefee3SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 278caefee3SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 288caefee3SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 298caefee3SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 308caefee3SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 318caefee3SMatthias Ringwald * SUCH DAMAGE. 328caefee3SMatthias Ringwald * 338caefee3SMatthias Ringwald * Please inquire about commercial licensing options at 348caefee3SMatthias Ringwald * [email protected] 358caefee3SMatthias Ringwald * 368caefee3SMatthias Ringwald */ 378caefee3SMatthias Ringwald 38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "main.c" 39ab2c6ae4SMatthias Ringwald 408caefee3SMatthias Ringwald // ***************************************************************************** 418caefee3SMatthias Ringwald // 428caefee3SMatthias Ringwald // minimal setup for HCI code 438caefee3SMatthias Ringwald // 448caefee3SMatthias Ringwald // ***************************************************************************** 458caefee3SMatthias Ringwald 468caefee3SMatthias Ringwald #include <stdint.h> 478caefee3SMatthias Ringwald #include <stdio.h> 488caefee3SMatthias Ringwald #include <stdlib.h> 498caefee3SMatthias Ringwald #include <string.h> 508caefee3SMatthias Ringwald #include <signal.h> 518caefee3SMatthias Ringwald 527907f069SMatthias Ringwald #include "btstack_config.h" 538caefee3SMatthias Ringwald 54*f9563b89SMatthias Ringwald #include "bluetooth_company_id.h" 55a98592bcSMatthias Ringwald #include "btstack_debug.h" 56d356a6daSMatthias Ringwald #include "btstack_event.h" 57a98592bcSMatthias Ringwald #include "btstack_link_key_db_fs.h" 58a98592bcSMatthias Ringwald #include "btstack_memory.h" 5982636622SMatthias Ringwald #include "btstack_run_loop.h" 60b7d596c1SMatthias Ringwald #include "btstack_run_loop_posix.h" 618caefee3SMatthias Ringwald #include "hal_led.h" 628caefee3SMatthias Ringwald #include "hci.h" 638caefee3SMatthias Ringwald #include "hci_dump.h" 647ea7688aSMatthias Ringwald #include "btstack_stdin.h" 65e739af98SMatthias Ringwald #include "btstack_audio.h" 6659bdfe96SMatthias Ringwald #include "btstack_tlv_posix.h" 67*f9563b89SMatthias Ringwald #include "btstack_chipset_zephyr.h" 6859bdfe96SMatthias Ringwald 6959bdfe96SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_" 7059bdfe96SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv" 7159bdfe96SMatthias Ringwald static char tlv_db_path[100]; 7259bdfe96SMatthias Ringwald static const btstack_tlv_t * tlv_impl; 7359bdfe96SMatthias Ringwald static btstack_tlv_posix_t tlv_context; 7459bdfe96SMatthias Ringwald static bd_addr_t local_addr; 758caefee3SMatthias Ringwald 768caefee3SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 778caefee3SMatthias Ringwald 78*f9563b89SMatthias Ringwald static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc }; 79*f9563b89SMatthias Ringwald 80*f9563b89SMatthias Ringwald static bd_addr_t static_address; 81*f9563b89SMatthias Ringwald static int using_static_address; 82*f9563b89SMatthias Ringwald 83d356a6daSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 84d356a6daSMatthias Ringwald 85*f9563b89SMatthias Ringwald static void local_version_information_handler(uint8_t * packet){ 86*f9563b89SMatthias Ringwald printf("Local version information:\n"); 87*f9563b89SMatthias Ringwald uint16_t hci_version = packet[6]; 88*f9563b89SMatthias Ringwald uint16_t hci_revision = little_endian_read_16(packet, 7); 89*f9563b89SMatthias Ringwald uint16_t lmp_version = packet[9]; 90*f9563b89SMatthias Ringwald uint16_t manufacturer = little_endian_read_16(packet, 10); 91*f9563b89SMatthias Ringwald uint16_t lmp_subversion = little_endian_read_16(packet, 12); 92*f9563b89SMatthias Ringwald printf("- HCI Version 0x%04x\n", hci_version); 93*f9563b89SMatthias Ringwald printf("- HCI Revision 0x%04x\n", hci_revision); 94*f9563b89SMatthias Ringwald printf("- LMP Version 0x%04x\n", lmp_version); 95*f9563b89SMatthias Ringwald printf("- LMP Subversion 0x%04x\n", lmp_subversion); 96*f9563b89SMatthias Ringwald printf("- Manufacturer 0x%04x\n", manufacturer); 97*f9563b89SMatthias Ringwald switch (manufacturer){ 98*f9563b89SMatthias Ringwald case BLUETOOTH_COMPANY_ID_THE_LINUX_FOUNDATION: 99*f9563b89SMatthias Ringwald printf("Linux Foundation - assume Zephyr hci_usb example running on nRF52xx\n"); 100*f9563b89SMatthias Ringwald hci_set_chipset(btstack_chipset_zephyr_instance()); 101*f9563b89SMatthias Ringwald break; 102*f9563b89SMatthias Ringwald default: 103*f9563b89SMatthias Ringwald break; 104*f9563b89SMatthias Ringwald } 105*f9563b89SMatthias Ringwald } 106*f9563b89SMatthias Ringwald 107d356a6daSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1089ec2630cSMatthias Ringwald UNUSED(channel); 1099ec2630cSMatthias Ringwald UNUSED(size); 110d356a6daSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 11159bdfe96SMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 11259bdfe96SMatthias Ringwald case BTSTACK_EVENT_STATE: 113d356a6daSMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 11459bdfe96SMatthias Ringwald gap_local_bd_addr(local_addr); 115*f9563b89SMatthias Ringwald if (using_static_address){ 116*f9563b89SMatthias Ringwald memcpy(local_addr, static_address, 6); 117*f9563b89SMatthias Ringwald } 11859bdfe96SMatthias Ringwald printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 11959bdfe96SMatthias Ringwald strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); 12059bdfe96SMatthias Ringwald strcat(tlv_db_path, bd_addr_to_str(local_addr)); 12159bdfe96SMatthias Ringwald strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); 12259bdfe96SMatthias Ringwald tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 12359bdfe96SMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context); 12459bdfe96SMatthias Ringwald break; 125*f9563b89SMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 126*f9563b89SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){ 127*f9563b89SMatthias Ringwald local_version_information_handler(packet); 128*f9563b89SMatthias Ringwald } 129*f9563b89SMatthias Ringwald if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0){ 130*f9563b89SMatthias Ringwald reverse_48(&packet[7], static_address); 131*f9563b89SMatthias Ringwald gap_random_address_set(static_address); 132*f9563b89SMatthias Ringwald using_static_address = 1; 133*f9563b89SMatthias Ringwald } 134*f9563b89SMatthias Ringwald break; 13559bdfe96SMatthias Ringwald default: 13659bdfe96SMatthias Ringwald break; 13759bdfe96SMatthias Ringwald } 138d356a6daSMatthias Ringwald } 139d356a6daSMatthias Ringwald 1408caefee3SMatthias Ringwald static void sigint_handler(int param){ 1419ec2630cSMatthias Ringwald UNUSED(param); 1428caefee3SMatthias Ringwald 143b96c8f44SMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 144b96c8f44SMatthias Ringwald log_info("sigint_handler: shutting down"); 145b96c8f44SMatthias Ringwald 1468caefee3SMatthias Ringwald // reset anyway 1478caefee3SMatthias Ringwald btstack_stdin_reset(); 1488caefee3SMatthias Ringwald 149b96c8f44SMatthias Ringwald // power down 1508caefee3SMatthias Ringwald hci_power_control(HCI_POWER_OFF); 1518caefee3SMatthias Ringwald hci_close(); 1528caefee3SMatthias Ringwald log_info("Good bye, see you.\n"); 1538caefee3SMatthias Ringwald exit(0); 1548caefee3SMatthias Ringwald } 1558caefee3SMatthias Ringwald 1568caefee3SMatthias Ringwald static int led_state = 0; 1578caefee3SMatthias Ringwald void hal_led_toggle(void){ 1588caefee3SMatthias Ringwald led_state = 1 - led_state; 1598caefee3SMatthias Ringwald printf("LED State %u\n", led_state); 1608caefee3SMatthias Ringwald } 1618caefee3SMatthias Ringwald 162e13e7f54SMatthias Ringwald #define USB_MAX_PATH_LEN 7 1638caefee3SMatthias Ringwald int main(int argc, const char * argv[]){ 1648caefee3SMatthias Ringwald 165e13e7f54SMatthias Ringwald uint8_t usb_path[USB_MAX_PATH_LEN]; 166e13e7f54SMatthias Ringwald int usb_path_len = 0; 167fb34235aSMatthias Ringwald const char * usb_path_string = NULL; 168e13e7f54SMatthias Ringwald if (argc >= 3 && strcmp(argv[1], "-u") == 0){ 169e13e7f54SMatthias Ringwald // parse command line options for "-u 11:22:33" 170fb34235aSMatthias Ringwald usb_path_string = argv[2]; 171e13e7f54SMatthias Ringwald printf("Specified USB Path: "); 172e13e7f54SMatthias Ringwald while (1){ 173e13e7f54SMatthias Ringwald char * delimiter; 174fb34235aSMatthias Ringwald int port = strtol(usb_path_string, &delimiter, 16); 175e13e7f54SMatthias Ringwald usb_path[usb_path_len] = port; 176e13e7f54SMatthias Ringwald usb_path_len++; 177e13e7f54SMatthias Ringwald printf("%02x ", port); 178e13e7f54SMatthias Ringwald if (!delimiter) break; 179a4c4a5d6SMatthias Ringwald if (*delimiter != ':' && *delimiter != '-') break; 180fb34235aSMatthias Ringwald usb_path_string = delimiter+1; 181e13e7f54SMatthias Ringwald } 182e13e7f54SMatthias Ringwald printf("\n"); 1837c3351a5SMatthias Ringwald argc -= 2; 184fb34235aSMatthias Ringwald memmove(&argv[1], &argv[3], (argc-1) * sizeof(char *)); 185e13e7f54SMatthias Ringwald } 186e13e7f54SMatthias Ringwald 1878caefee3SMatthias Ringwald /// GET STARTED with BTstack /// 1888caefee3SMatthias Ringwald btstack_memory_init(); 189528a4a3bSMatthias Ringwald btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 1908caefee3SMatthias Ringwald 191e13e7f54SMatthias Ringwald if (usb_path_len){ 192e13e7f54SMatthias Ringwald hci_transport_usb_set_path(usb_path_len, usb_path); 193e13e7f54SMatthias Ringwald } 194e13e7f54SMatthias Ringwald 1958caefee3SMatthias Ringwald // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT 19602065453SMatthias Ringwald 19702065453SMatthias Ringwald char pklg_path[100]; 19802065453SMatthias Ringwald strcpy(pklg_path, "/tmp/hci_dump"); 19902065453SMatthias Ringwald if (usb_path_len){ 20002065453SMatthias Ringwald strcat(pklg_path, "_"); 201fb34235aSMatthias Ringwald strcat(pklg_path, usb_path_string); 20202065453SMatthias Ringwald } 20302065453SMatthias Ringwald strcat(pklg_path, ".pklg"); 20402065453SMatthias Ringwald printf("Packet Log: %s\n", pklg_path); 20502065453SMatthias Ringwald hci_dump_open(pklg_path, HCI_DUMP_PACKETLOGGER); 2068caefee3SMatthias Ringwald 2078caefee3SMatthias Ringwald // init HCI 2082d5e09d6SMatthias Ringwald hci_init(hci_transport_usb_instance(), NULL); 20935454696SMatthias Ringwald 21035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2112d5e09d6SMatthias Ringwald hci_set_link_key_db(btstack_link_key_db_fs_instance()); 21235454696SMatthias Ringwald #endif 2138caefee3SMatthias Ringwald 214e739af98SMatthias Ringwald #ifdef HAVE_PORTAUDIO 2153d3351f3SMatthias Ringwald btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance()); 2163d3351f3SMatthias Ringwald btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance()); 217e739af98SMatthias Ringwald #endif 218e739af98SMatthias Ringwald 219d356a6daSMatthias Ringwald // inform about BTstack state 220d356a6daSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 221d356a6daSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 222d356a6daSMatthias Ringwald 2238caefee3SMatthias Ringwald // handle CTRL-c 2248caefee3SMatthias Ringwald signal(SIGINT, sigint_handler); 2258caefee3SMatthias Ringwald 2268caefee3SMatthias Ringwald // setup app 2278caefee3SMatthias Ringwald btstack_main(argc, argv); 2288caefee3SMatthias Ringwald 2298caefee3SMatthias Ringwald // go 230528a4a3bSMatthias Ringwald btstack_run_loop_execute(); 2318caefee3SMatthias Ringwald 2328caefee3SMatthias Ringwald return 0; 2338caefee3SMatthias Ringwald } 234