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 MATTHIAS 24 * RINGWALD 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 #include <getopt.h> 46 #include <errno.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 "bluetooth_company_id.h" 57 #include "btstack_debug.h" 58 #include "btstack_event.h" 59 #include "ble/le_device_db_tlv.h" 60 #include "classic/btstack_link_key_db_tlv.h" 61 #include "btstack_memory.h" 62 #include "btstack_run_loop.h" 63 #include "btstack_run_loop_posix.h" 64 #include "hal_led.h" 65 #include "hci.h" 66 #include "hci_transport.h" 67 #include "hci_transport_usb.h" 68 #include "hci_dump.h" 69 #include "hci_dump_posix_fs.h" 70 #include "btstack_stdin.h" 71 #include "btstack_audio.h" 72 #include "btstack_tlv_posix.h" 73 #include "btstack_chipset_zephyr.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 bool tlv_clear = false; 79 static const btstack_tlv_t * tlv_impl; 80 static btstack_tlv_posix_t tlv_context; 81 static bd_addr_t local_addr; 82 83 #define MAX_CMD_LINE_ITEMS 100 84 static int app_argc = 0; 85 static const char *app_argv[MAX_CMD_LINE_ITEMS] = { NULL }; 86 87 int btstack_main(int argc, const char * argv[]); 88 89 static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc }; 90 91 static bd_addr_t static_address; 92 static int using_static_address; 93 94 static btstack_packet_callback_registration_t hci_event_callback_registration; 95 96 static void local_version_information_handler(uint8_t * packet){ 97 printf("Local version information:\n"); 98 uint16_t hci_version = packet[6]; 99 uint16_t hci_revision = little_endian_read_16(packet, 7); 100 uint16_t lmp_version = packet[9]; 101 uint16_t manufacturer = little_endian_read_16(packet, 10); 102 uint16_t lmp_subversion = little_endian_read_16(packet, 12); 103 printf("- HCI Version 0x%04x\n", hci_version); 104 printf("- HCI Revision 0x%04x\n", hci_revision); 105 printf("- LMP Version 0x%04x\n", lmp_version); 106 printf("- LMP Subversion 0x%04x\n", lmp_subversion); 107 printf("- Manufacturer 0x%04x\n", manufacturer); 108 switch (manufacturer){ 109 case BLUETOOTH_COMPANY_ID_THE_LINUX_FOUNDATION: 110 printf("Linux Foundation - assume Zephyr hci_usb example running on nRF52xx\n"); 111 hci_set_chipset(btstack_chipset_zephyr_instance()); 112 break; 113 default: 114 break; 115 } 116 } 117 118 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 119 UNUSED(channel); 120 UNUSED(size); 121 uint8_t usb_path_len; 122 const uint8_t * usb_path; 123 uint16_t product_id; 124 uint8_t i; 125 126 if (packet_type != HCI_EVENT_PACKET) return; 127 switch (hci_event_packet_get_type(packet)){ 128 case BTSTACK_EVENT_STATE: 129 switch (btstack_event_state_get_state(packet)){ 130 case HCI_STATE_WORKING: 131 gap_local_bd_addr(local_addr); 132 if (using_static_address){ 133 memcpy(local_addr, static_address, 6); 134 } 135 printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 136 btstack_strcpy(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_PREFIX); 137 btstack_strcat(tlv_db_path, sizeof(tlv_db_path), bd_addr_to_str(local_addr)); 138 btstack_strcat(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_POSTFIX); 139 printf("TLV path: %s", tlv_db_path); 140 if (tlv_clear){ 141 int rc = unlink(tlv_db_path); 142 if (rc == 0) { 143 printf(", reset ok"); 144 } else { 145 printf(", reset failed with result = %d", rc); 146 } 147 } 148 printf("\n"); 149 tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 150 btstack_tlv_set_instance(tlv_impl, &tlv_context); 151 #ifdef ENABLE_CLASSIC 152 hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); 153 #endif 154 #ifdef ENABLE_BLE 155 le_device_db_tlv_configure(tlv_impl, &tlv_context); 156 #endif 157 break; 158 case HCI_STATE_OFF: 159 btstack_tlv_posix_deinit(&tlv_context); 160 break; 161 default: 162 break; 163 } 164 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 165 break; 166 case HCI_EVENT_COMMAND_COMPLETE: 167 if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){ 168 local_version_information_handler(packet); 169 } 170 if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0){ 171 reverse_48(&packet[7], static_address); 172 gap_random_address_set(static_address); 173 using_static_address = 1; 174 } 175 break; 176 case HCI_EVENT_TRANSPORT_USB_INFO: 177 usb_path_len = hci_event_transport_usb_info_get_path_len(packet); 178 usb_path = hci_event_transport_usb_info_get_path(packet); 179 // print device path 180 product_id = hci_event_transport_usb_info_get_product_id(packet); 181 printf("USB device 0x%04x/0x%04x, path: ", 182 hci_event_transport_usb_info_get_vendor_id(packet), product_id); 183 for (i=0;i<usb_path_len;i++){ 184 if (i) printf("-"); 185 printf("%02x", usb_path[i]); 186 } 187 printf("\n"); 188 break; 189 190 default: 191 break; 192 } 193 } 194 195 static void sigint_handler(int param){ 196 UNUSED(param); 197 198 printf("CTRL-C - SIGINT received, shutting down..\n"); 199 log_info("sigint_handler: shutting down"); 200 201 // reset anyway 202 btstack_stdin_reset(); 203 204 // power down 205 hci_power_control(HCI_POWER_OFF); 206 hci_close(); 207 log_info("Good bye, see you.\n"); 208 exit(0); 209 } 210 211 static int led_state = 0; 212 void hal_led_toggle(void){ 213 led_state = 1 - led_state; 214 printf("LED State %u\n", led_state); 215 } 216 217 static void btstack_log_cmd_line( int argc, const char *args[] ) 218 { 219 char buf[2048] = "command line:"; 220 char *ptr = buf+strlen(buf); 221 size_t len = sizeof(buf); 222 for( int i=0; i<argc; ++i ) 223 { 224 int ret = snprintf(ptr, len, " %s", args[i]); 225 // avoid overflow 226 if((ret < 0) || ( (size_t)ret >= len )) 227 { 228 log_error("command line truncated!"); 229 break; 230 } 231 // terminate string 232 ptr[ret] = 0; 233 len -= ret; 234 ptr += ret; 235 } 236 log_info("%s", buf); 237 238 } 239 240 static const char short_options[] = "-hu:l:c"; 241 242 static const struct option long_options[] = { 243 {"help", no_argument, NULL, 'h'}, 244 {"logfile", required_argument, NULL, 'l'}, 245 {"clear-tlv", no_argument, NULL, 'c'}, 246 {"usbpath", required_argument, NULL, 'u'}, 247 {0, 0, 0, 0} 248 }; 249 250 static const char *help_options[] = { 251 "print (this) help.", 252 "set file to store debug output and HCI trace.", 253 "clear bonding information stored in TLV.", 254 "set USB path to Bluetooth Controller.", 255 }; 256 257 static const char *option_arg_name[] = { 258 "", 259 "LOGFILE", 260 "", 261 "USBPATH", 262 }; 263 264 static void usage(const char *name){ 265 unsigned int i; 266 printf( "usage:\n\t%s [options]\n", name ); 267 printf("valid options:\n"); 268 for( i=0; long_options[i].name != 0; i++) { 269 printf("--%-10s| -%c %-10s\t\t%s\n", long_options[i].name, long_options[i].val, option_arg_name[i], help_options[i] ); 270 } 271 } 272 273 static void clear_argv(int idx){ 274 app_argv[idx] = NULL; 275 } 276 277 static void shift_argv(int idx){ 278 for( int i=idx; i<app_argc-1; ++i ) 279 { 280 app_argv[i] = app_argv[i+1]; 281 } 282 app_argc--; 283 } 284 285 static void cleanup_argv(void){ 286 for( int i=0; i<app_argc; ){ 287 if( app_argv[i] == NULL ) 288 shift_argv( i ); 289 else{ 290 ++i; 291 } 292 } 293 } 294 295 #define USB_MAX_PATH_LEN 7 296 int main(int argc, const char * argv[]){ 297 298 uint8_t usb_path[USB_MAX_PATH_LEN]; 299 int usb_path_len = 0; 300 const char * usb_path_string = NULL; 301 const char * log_file_path = NULL; 302 303 btstack_assert( argc < MAX_CMD_LINE_ITEMS ); 304 app_argc = argc; 305 for( int i=0; i<argc; ++i ){ 306 app_argv[i] = argv[i]; 307 } 308 309 opterr = 0; // disable error message on unknown options 310 // parse command line parameters 311 while(true){ 312 int c = getopt_long( argc, (char* const *)argv, short_options, long_options, NULL ); 313 if (c < 0) { 314 break; 315 } 316 switch (c) { 317 case 'u': 318 usb_path_string = optarg; 319 clear_argv( optind - 1 ); 320 clear_argv( optind - 2 ); 321 break; 322 case 'l': 323 log_file_path = optarg; 324 clear_argv( optind - 1 ); 325 clear_argv( optind - 2 ); 326 break; 327 case 'c': 328 tlv_clear = true; 329 clear_argv( optind - 1 ); 330 break; 331 case '?': 332 case 1: 333 break; 334 case 'h': 335 default: 336 usage(argv[0]); 337 return EXIT_FAILURE; 338 } 339 } 340 341 if (usb_path_string != NULL){ 342 // parse command line options for "-u 11:22:33" 343 printf("Specified USB Path: "); 344 while (1){ 345 char * delimiter; 346 int port = strtol(usb_path_string, &delimiter, 16); 347 usb_path[usb_path_len] = port; 348 usb_path_len++; 349 printf("%02x ", port); 350 if (!delimiter) break; 351 if (*delimiter != ':' && *delimiter != '-') break; 352 usb_path_string = delimiter+1; 353 } 354 printf("\n"); 355 } 356 357 // remove used up command line args 358 cleanup_argv(); 359 360 /// GET STARTED with BTstack /// 361 btstack_memory_init(); 362 btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 363 364 if (usb_path_len){ 365 hci_transport_usb_set_path(usb_path_len, usb_path); 366 } 367 368 // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT 369 char pklg_path[100]; 370 btstack_strcpy(pklg_path, sizeof(pklg_path), "/tmp/hci_dump"); 371 if (usb_path_len){ 372 btstack_strcat(pklg_path, sizeof(pklg_path), "_"); 373 btstack_strcat(pklg_path, sizeof(pklg_path), usb_path_string); 374 } 375 btstack_strcat(pklg_path, sizeof(pklg_path), ".pklg"); 376 377 // use path from command line if given 378 if( log_file_path != NULL ){ 379 btstack_strcpy(pklg_path, sizeof(pklg_path), log_file_path ); 380 } 381 382 // log into file using HCI_DUMP_PACKETLOGGER format 383 hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER); 384 hci_dump_init(hci_dump_posix_fs_get_instance()); 385 printf("Packet Log: %s\n", pklg_path); 386 387 // let the log contain the calling parameters 388 btstack_log_cmd_line( app_argc, app_argv ); 389 390 // init HCI 391 hci_init(hci_transport_usb_instance(), NULL); 392 393 #ifdef HAVE_PORTAUDIO 394 btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance()); 395 btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance()); 396 #endif 397 398 // inform about BTstack state 399 hci_event_callback_registration.callback = &packet_handler; 400 hci_add_event_handler(&hci_event_callback_registration); 401 402 // handle CTRL-c 403 signal(SIGINT, sigint_handler); 404 405 // setup app 406 btstack_main(app_argc, app_argv); 407 408 // go 409 btstack_run_loop_execute(); 410 411 return 0; 412 } 413