1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define BTSTACK_FILE__ "main.c" 39 40 // ***************************************************************************** 41 // 42 // minimal setup for HCI code 43 // 44 // ***************************************************************************** 45 46 #include <stdint.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <signal.h> 51 52 #include "btstack_config.h" 53 54 #include "ble/le_device_db_tlv.h" 55 #include "bluetooth_company_id.h" 56 #include "btstack_audio.h" 57 #include "btstack_chipset_realtek.h" 58 #include "btstack_chipset_zephyr.h" 59 #include "btstack_debug.h" 60 #include "btstack_event.h" 61 #include "btstack_memory.h" 62 #include "btstack_run_loop.h" 63 #include "btstack_run_loop_posix.h" 64 #include "btstack_signal.h" 65 #include "btstack_stdin.h" 66 #include "btstack_tlv_posix.h" 67 #include "classic/btstack_link_key_db_tlv.h" 68 #include "hal_led.h" 69 #include "hci.h" 70 #include "hci_dump.h" 71 #include "hci_dump_posix_fs.h" 72 #include "hci_transport.h" 73 #include "hci_transport_usb.h" 74 75 #define TLV_DB_PATH_PREFIX "/tmp/btstack_" 76 #define TLV_DB_PATH_POSTFIX ".tlv" 77 static char tlv_db_path[100]; 78 static const btstack_tlv_t * tlv_impl; 79 static btstack_tlv_posix_t tlv_context; 80 static bd_addr_t local_addr; 81 82 int btstack_main(int argc, const char * argv[]); 83 84 static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc }; 85 86 static bd_addr_t static_address; 87 static int using_static_address; 88 89 static btstack_packet_callback_registration_t hci_event_callback_registration; 90 91 // shutdown 92 static bool shutdown_triggered; 93 94 static void local_version_information_handler(uint8_t * packet){ 95 printf("Local version information:\n"); 96 uint16_t hci_version = packet[6]; 97 uint16_t hci_revision = little_endian_read_16(packet, 7); 98 uint16_t lmp_version = packet[9]; 99 uint16_t manufacturer = little_endian_read_16(packet, 10); 100 uint16_t lmp_subversion = little_endian_read_16(packet, 12); 101 printf("- HCI Version 0x%04x\n", hci_version); 102 printf("- HCI Revision 0x%04x\n", hci_revision); 103 printf("- LMP Version 0x%04x\n", lmp_version); 104 printf("- LMP Subversion 0x%04x\n", lmp_subversion); 105 printf("- Manufacturer 0x%04x\n", manufacturer); 106 switch (manufacturer){ 107 case BLUETOOTH_COMPANY_ID_THE_LINUX_FOUNDATION: 108 printf("- Linux Foundation - assume Zephyr hci_usb firmware running on nRF52xx\n"); 109 // setup Zephyr chipset support 110 hci_set_chipset(btstack_chipset_zephyr_instance()); 111 // sm required to setup static random Bluetooth address 112 sm_init(); 113 break; 114 case BLUETOOTH_COMPANY_ID_REALTEK_SEMICONDUCTOR_CORPORATION: 115 printf("- Realtek controller - provide firmware and config\n"); 116 btstack_chipset_realtek_set_lmp_subversion(lmp_subversion); 117 hci_set_chipset(btstack_chipset_realtek_instance()); 118 break; 119 default: 120 break; 121 } 122 } 123 124 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 125 UNUSED(channel); 126 UNUSED(size); 127 uint8_t i; 128 uint8_t usb_path_len; 129 const uint8_t * usb_path; 130 uint16_t product_id; 131 132 if (packet_type != HCI_EVENT_PACKET) return; 133 134 switch (hci_event_packet_get_type(packet)){ 135 case HCI_EVENT_TRANSPORT_USB_INFO: 136 usb_path_len = hci_event_transport_usb_info_get_path_len(packet); 137 usb_path = hci_event_transport_usb_info_get_path(packet); 138 // print device path 139 product_id = hci_event_transport_usb_info_get_product_id(packet); 140 printf("USB device 0x%04x/0x%04x, path: ", 141 hci_event_transport_usb_info_get_vendor_id(packet), product_id); 142 for (i=0;i<usb_path_len;i++){ 143 if (i) printf("-"); 144 printf("%02x", usb_path[i]); 145 } 146 printf("\n"); 147 // set Product ID for Realtek Controllers 148 btstack_chipset_realtek_set_product_id(product_id); 149 break; 150 case BTSTACK_EVENT_STATE: 151 switch (btstack_event_state_get_state(packet)){ 152 case HCI_STATE_WORKING: 153 gap_local_bd_addr(local_addr); 154 if (using_static_address){ 155 memcpy(local_addr, static_address, 6); 156 } 157 printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 158 btstack_strcpy(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_PREFIX); 159 btstack_strcat(tlv_db_path, sizeof(tlv_db_path), bd_addr_to_str_with_delimiter(local_addr, '-')); 160 btstack_strcat(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_POSTFIX); 161 tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 162 btstack_tlv_set_instance(tlv_impl, &tlv_context); 163 #ifdef ENABLE_CLASSIC 164 hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); 165 #endif 166 #ifdef ENABLE_BLE 167 le_device_db_tlv_configure(tlv_impl, &tlv_context); 168 #endif 169 break; 170 case HCI_STATE_OFF: 171 btstack_tlv_posix_deinit(&tlv_context); 172 if (!shutdown_triggered) break; 173 // reset stdin 174 btstack_stdin_reset(); 175 log_info("Good bye, see you.\n"); 176 exit(0); 177 break; 178 default: 179 break; 180 } 181 break; 182 case HCI_EVENT_COMMAND_COMPLETE: 183 if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){ 184 local_version_information_handler(packet); 185 } 186 if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0){ 187 reverse_48(&packet[7], static_address); 188 gap_random_address_set(static_address); 189 using_static_address = 1; 190 } 191 break; 192 default: 193 break; 194 } 195 } 196 197 static void trigger_shutdown(void){ 198 printf("CTRL-C - SIGINT received, shutting down..\n"); 199 log_info("sigint_handler: shutting down"); 200 shutdown_triggered = true; 201 hci_power_control(HCI_POWER_OFF); 202 } 203 204 static int led_state = 0; 205 void hal_led_toggle(void){ 206 led_state = 1 - led_state; 207 printf("LED State %u\n", led_state); 208 } 209 210 #define USB_MAX_PATH_LEN 7 211 int main(int argc, const char * argv[]){ 212 213 uint8_t usb_path[USB_MAX_PATH_LEN]; 214 int usb_path_len = 0; 215 const char * usb_path_string = NULL; 216 if (argc >= 3 && strcmp(argv[1], "-u") == 0){ 217 // parse command line options for "-u 11:22:33" 218 usb_path_string = argv[2]; 219 printf("Specified USB Path: "); 220 while (1){ 221 char * delimiter; 222 int port = strtol(usb_path_string, &delimiter, 16); 223 usb_path[usb_path_len] = port; 224 usb_path_len++; 225 printf("%02x ", port); 226 if (!delimiter) break; 227 if (*delimiter != ':' && *delimiter != '-') break; 228 usb_path_string = delimiter+1; 229 } 230 printf("\n"); 231 argc -= 2; 232 memmove((void *) &argv[1], &argv[3], (argc-1) * sizeof(char *)); 233 } 234 235 /// GET STARTED with BTstack /// 236 btstack_memory_init(); 237 btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 238 239 if (usb_path_len){ 240 hci_transport_usb_set_path(usb_path_len, usb_path); 241 } 242 243 // log into file using HCI_DUMP_PACKETLOGGER format 244 char pklg_path[100]; 245 btstack_strcpy(pklg_path, sizeof(pklg_path), "/tmp/hci_dump"); 246 if (usb_path_len){ 247 btstack_strcat(pklg_path, sizeof(pklg_path), "_"); 248 btstack_strcat(pklg_path, sizeof(pklg_path), usb_path_string); 249 } 250 btstack_strcat(pklg_path, sizeof(pklg_path), ".pklg"); 251 hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER); 252 const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance(); 253 hci_dump_init(hci_dump_impl); 254 printf("Packet Log: %s\n", pklg_path); 255 256 // init HCI 257 hci_init(hci_transport_usb_instance(), NULL); 258 259 #ifdef HAVE_PORTAUDIO 260 btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance()); 261 btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance()); 262 #endif 263 264 // inform about BTstack state 265 hci_event_callback_registration.callback = &packet_handler; 266 hci_add_event_handler(&hci_event_callback_registration); 267 268 // register callback for CTRL-c 269 btstack_signal_register_callback(SIGINT, &trigger_shutdown); 270 271 // register known Realtek USB Controllers 272 uint16_t realtek_num_controllers = btstack_chipset_realtek_get_num_usb_controllers(); 273 uint16_t i; 274 for (i=0;i<realtek_num_controllers;i++){ 275 uint16_t vendor_id; 276 uint16_t product_id; 277 btstack_chipset_realtek_get_vendor_product_id(i, &vendor_id, &product_id); 278 hci_transport_usb_add_device(vendor_id, product_id); 279 } 280 281 // setup app 282 btstack_main(argc, argv); 283 284 // go 285 btstack_run_loop_execute(); 286 287 return 0; 288 } 289