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 <getopt.h> 47 #include <stdint.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <signal.h> 52 #include <unistd.h> 53 54 #include "btstack_config.h" 55 56 #include "ble/le_device_db_tlv.h" 57 #include "bluetooth_company_id.h" 58 #include "btstack_audio.h" 59 #include "btstack_chipset_realtek.h" 60 #include "btstack_chipset_zephyr.h" 61 #include "btstack_debug.h" 62 #include "btstack_event.h" 63 #include "btstack_memory.h" 64 #include "btstack_run_loop.h" 65 #include "btstack_run_loop_posix.h" 66 #include "btstack_signal.h" 67 #include "btstack_stdin.h" 68 #include "btstack_tlv_posix.h" 69 #include "classic/btstack_link_key_db_tlv.h" 70 #include "hal_led.h" 71 #include "hci.h" 72 #include "hci_dump.h" 73 #include "hci_dump_posix_fs.h" 74 #include "hci_transport.h" 75 #include "hci_transport_usb.h" 76 77 #define TLV_DB_PATH_PREFIX "/tmp/btstack_" 78 #define TLV_DB_PATH_POSTFIX ".tlv" 79 static char tlv_db_path[100]; 80 static bool tlv_reset; 81 static const btstack_tlv_t * tlv_impl; 82 static btstack_tlv_posix_t tlv_context; 83 static bd_addr_t local_addr; 84 85 int btstack_main(int argc, const char * argv[]); 86 87 static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc }; 88 89 static bd_addr_t static_address; 90 static int using_static_address; 91 92 static btstack_packet_callback_registration_t hci_event_callback_registration; 93 94 // shutdown 95 static bool shutdown_triggered; 96 97 static void local_version_information_handler(uint8_t * packet){ 98 printf("Local version information:\n"); 99 uint16_t hci_version = packet[6]; 100 uint16_t hci_revision = little_endian_read_16(packet, 7); 101 uint16_t lmp_version = packet[9]; 102 uint16_t manufacturer = little_endian_read_16(packet, 10); 103 uint16_t lmp_subversion = little_endian_read_16(packet, 12); 104 printf("- HCI Version 0x%04x\n", hci_version); 105 printf("- HCI Revision 0x%04x\n", hci_revision); 106 printf("- LMP Version 0x%04x\n", lmp_version); 107 printf("- LMP Subversion 0x%04x\n", lmp_subversion); 108 printf("- Manufacturer 0x%04x\n", manufacturer); 109 switch (manufacturer){ 110 case BLUETOOTH_COMPANY_ID_THE_LINUX_FOUNDATION: 111 printf("- Linux Foundation - assume Zephyr hci_usb firmware running on nRF52xx\n"); 112 // setup Zephyr chipset support 113 hci_set_chipset(btstack_chipset_zephyr_instance()); 114 // sm required to setup static random Bluetooth address 115 sm_init(); 116 break; 117 case BLUETOOTH_COMPANY_ID_REALTEK_SEMICONDUCTOR_CORPORATION: 118 printf("- Realtek controller - provide firmware and config\n"); 119 btstack_chipset_realtek_set_lmp_subversion(lmp_subversion); 120 hci_set_chipset(btstack_chipset_realtek_instance()); 121 break; 122 default: 123 break; 124 } 125 } 126 127 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 128 UNUSED(channel); 129 UNUSED(size); 130 uint8_t i; 131 uint8_t usb_path_len; 132 const uint8_t * usb_path; 133 uint16_t product_id; 134 135 if (packet_type != HCI_EVENT_PACKET) return; 136 137 switch (hci_event_packet_get_type(packet)){ 138 case HCI_EVENT_TRANSPORT_USB_INFO: 139 usb_path_len = hci_event_transport_usb_info_get_path_len(packet); 140 usb_path = hci_event_transport_usb_info_get_path(packet); 141 // print device path 142 product_id = hci_event_transport_usb_info_get_product_id(packet); 143 printf("USB device 0x%04x/0x%04x, path: ", 144 hci_event_transport_usb_info_get_vendor_id(packet), product_id); 145 for (i=0;i<usb_path_len;i++){ 146 if (i) printf("-"); 147 printf("%02x", usb_path[i]); 148 } 149 printf("\n"); 150 // set Product ID for Realtek Controllers 151 btstack_chipset_realtek_set_product_id(product_id); 152 break; 153 case BTSTACK_EVENT_STATE: 154 switch (btstack_event_state_get_state(packet)){ 155 case HCI_STATE_WORKING: 156 gap_local_bd_addr(local_addr); 157 if (using_static_address){ 158 memcpy(local_addr, static_address, 6); 159 } 160 btstack_strcpy(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_PREFIX); 161 btstack_strcat(tlv_db_path, sizeof(tlv_db_path), bd_addr_to_str_with_delimiter(local_addr, '-')); 162 btstack_strcat(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_POSTFIX); 163 printf("TLV path: %s", tlv_db_path); 164 if (tlv_reset){ 165 int rc = unlink(tlv_db_path); 166 if (rc == 0) { 167 printf(", reset ok"); 168 } else { 169 printf(", reset failed with result = %d", rc); 170 } 171 } 172 printf("\n"); 173 tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 174 btstack_tlv_set_instance(tlv_impl, &tlv_context); 175 #ifdef ENABLE_CLASSIC 176 hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); 177 #endif 178 #ifdef ENABLE_BLE 179 le_device_db_tlv_configure(tlv_impl, &tlv_context); 180 #endif 181 printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 182 break; 183 case HCI_STATE_OFF: 184 btstack_tlv_posix_deinit(&tlv_context); 185 if (!shutdown_triggered) break; 186 // reset stdin 187 btstack_stdin_reset(); 188 log_info("Good bye, see you.\n"); 189 exit(0); 190 break; 191 default: 192 break; 193 } 194 break; 195 case HCI_EVENT_COMMAND_COMPLETE: 196 if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){ 197 local_version_information_handler(packet); 198 } 199 if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0){ 200 reverse_48(&packet[7], static_address); 201 gap_random_address_set(static_address); 202 using_static_address = 1; 203 } 204 break; 205 default: 206 break; 207 } 208 } 209 210 static void trigger_shutdown(void){ 211 printf("CTRL-C - SIGINT received, shutting down..\n"); 212 log_info("sigint_handler: shutting down"); 213 shutdown_triggered = true; 214 hci_power_control(HCI_POWER_OFF); 215 } 216 217 static int led_state = 0; 218 219 void hal_led_toggle(void){ 220 led_state = 1 - led_state; 221 printf("LED State %u\n", led_state); 222 } 223 224 static char short_options[] = "hu:l:r"; 225 226 static struct option long_options[] = { 227 {"help", no_argument, NULL, 'h'}, 228 {"logfile", required_argument, NULL, 'l'}, 229 {"reset-tlv", no_argument, NULL, 'r'}, 230 {"usbpath", required_argument, NULL, 'u'}, 231 {0, 0, 0, 0} 232 }; 233 234 static char *help_options[] = { 235 "print (this) help.", 236 "set file to store debug output and HCI trace.", 237 "reset bonding information stored in TLV.", 238 "set USB path to Bluetooth Controller.", 239 }; 240 241 static char *option_arg_name[] = { 242 "", 243 "LOGFILE", 244 "", 245 "USBPATH", 246 }; 247 248 static void usage(const char *name){ 249 unsigned int i; 250 printf( "usage:\n\t%s [options]\n", name ); 251 printf("valid options:\n"); 252 for( i=0; long_options[i].name != 0; i++) { 253 printf("--%-10s| -%c %-10s\t\t%s\n", long_options[i].name, long_options[i].val, option_arg_name[i], help_options[i] ); 254 } 255 } 256 257 #define USB_MAX_PATH_LEN 7 258 int main(int argc, const char * argv[]){ 259 260 uint8_t usb_path[USB_MAX_PATH_LEN]; 261 int usb_path_len = 0; 262 const char * usb_path_string = NULL; 263 const char * log_file_path = NULL; 264 265 // parse command line parameters 266 while(true){ 267 int c = getopt_long( argc, (char* const *)argv, short_options, long_options, NULL ); 268 if (c < 0) { 269 break; 270 } 271 switch (c) { 272 case 'u': 273 usb_path_string = optarg; 274 break; 275 case 'l': 276 log_file_path = optarg; 277 break; 278 case 'r': 279 tlv_reset = true; 280 break; 281 case '?': 282 case 'h': 283 default: 284 usage(argv[0]); 285 return EXIT_FAILURE; 286 } 287 } 288 289 if (usb_path_string != NULL){ 290 // parse command line options for "-u 11:22:33" 291 printf("Specified USB Path: "); 292 while (1){ 293 char * delimiter; 294 int port = strtol(usb_path_string, &delimiter, 16); 295 usb_path[usb_path_len] = port; 296 usb_path_len++; 297 printf("%02x ", port); 298 if (!delimiter) break; 299 if (*delimiter != ':' && *delimiter != '-') break; 300 usb_path_string = delimiter+1; 301 } 302 printf("\n"); 303 } 304 305 /// GET STARTED with BTstack /// 306 btstack_memory_init(); 307 btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 308 309 if (usb_path_len){ 310 hci_transport_usb_set_path(usb_path_len, usb_path); 311 } 312 313 // log into file using HCI_DUMP_PACKETLOGGER format 314 char pklg_path[100]; 315 if (log_file_path == NULL){ 316 btstack_strcpy(pklg_path, sizeof(pklg_path), "/tmp/hci_dump"); 317 if (usb_path_len){ 318 btstack_strcat(pklg_path, sizeof(pklg_path), "_"); 319 btstack_strcat(pklg_path, sizeof(pklg_path), usb_path_string); 320 } 321 btstack_strcat(pklg_path, sizeof(pklg_path), ".pklg"); 322 log_file_path = pklg_path; 323 } 324 325 hci_dump_posix_fs_open(log_file_path, HCI_DUMP_PACKETLOGGER); 326 const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance(); 327 hci_dump_init(hci_dump_impl); 328 printf("Packet Log: %s\n", log_file_path); 329 330 // init HCI 331 hci_init(hci_transport_usb_instance(), NULL); 332 333 #ifdef HAVE_PORTAUDIO 334 btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance()); 335 btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance()); 336 #endif 337 338 // inform about BTstack state 339 hci_event_callback_registration.callback = &packet_handler; 340 hci_add_event_handler(&hci_event_callback_registration); 341 342 // register callback for CTRL-c 343 btstack_signal_register_callback(SIGINT, &trigger_shutdown); 344 345 // register known Realtek USB Controllers 346 uint16_t realtek_num_controllers = btstack_chipset_realtek_get_num_usb_controllers(); 347 uint16_t i; 348 for (i=0;i<realtek_num_controllers;i++){ 349 uint16_t vendor_id; 350 uint16_t product_id; 351 btstack_chipset_realtek_get_vendor_product_id(i, &vendor_id, &product_id); 352 hci_transport_usb_add_device(vendor_id, product_id); 353 } 354 355 // setup app 356 btstack_main(argc, argv); 357 358 // go 359 btstack_run_loop_execute(); 360 361 return 0; 362 } 363