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 #include <unistd.h> 52 #include <getopt.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_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 "btstack_uart.h" 68 #include "classic/btstack_link_key_db_tlv.h" 69 #include "hci.h" 70 #include "hci_dump.h" 71 #include "hci_dump_posix_fs.h" 72 #include "hci_dump_posix_stdout.h" 73 #include "hci_transport_netgraph.h" 74 75 76 #define TLV_DB_PATH_PREFIX "/tmp/btstack_" 77 #define TLV_DB_PATH_POSTFIX ".tlv" 78 static char tlv_db_path[100]; 79 static bool tlv_reset; 80 static const btstack_tlv_t * tlv_impl; 81 static btstack_tlv_posix_t tlv_context; 82 static bd_addr_t local_addr; 83 static hci_transport_config_netgraph_t config; 84 85 // shutdown 86 static bool shutdown_triggered; 87 88 int btstack_main(int argc, const char * argv[]); 89 static void local_version_information_handler(uint8_t * packet); 90 91 static btstack_packet_callback_registration_t hci_event_callback_registration; 92 93 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 94 if (packet_type != HCI_EVENT_PACKET) return; 95 switch (hci_event_packet_get_type(packet)){ 96 case BTSTACK_EVENT_STATE: 97 switch(btstack_event_state_get_state(packet)){ 98 case HCI_STATE_WORKING: 99 gap_local_bd_addr(local_addr); 100 printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 101 btstack_strcpy(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_PREFIX); 102 btstack_strcat(tlv_db_path, sizeof(tlv_db_path), bd_addr_to_str_with_delimiter(local_addr, '-')); 103 btstack_strcat(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_POSTFIX); 104 printf("TLV path: %s", tlv_db_path); 105 if (tlv_reset){ 106 int rc = unlink(tlv_db_path); 107 if (rc == 0) { 108 printf(", reset ok"); 109 } else { 110 printf(", reset failed with result = %d", rc); 111 } 112 } 113 printf("\n"); 114 tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 115 btstack_tlv_set_instance(tlv_impl, &tlv_context); 116 #ifdef ENABLE_CLASSIC 117 hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); 118 #endif 119 #ifdef ENABLE_BLE 120 le_device_db_tlv_configure(tlv_impl, &tlv_context); 121 #endif 122 break; 123 case HCI_STATE_OFF: 124 btstack_tlv_posix_deinit(&tlv_context); 125 if (!shutdown_triggered) break; 126 // reset stdin 127 btstack_stdin_reset(); 128 log_info("Good bye, see you.\n"); 129 exit(0); 130 break; 131 default: 132 break; 133 } 134 break; 135 case HCI_EVENT_COMMAND_COMPLETE: 136 if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){ 137 local_version_information_handler(packet); 138 } 139 break; 140 default: 141 break; 142 } 143 } 144 145 static void trigger_shutdown(void){ 146 printf("CTRL-C - SIGINT received, shutting down..\n"); 147 log_info("sigint_handler: shutting down"); 148 shutdown_triggered = true; 149 hci_power_control(HCI_POWER_OFF); 150 } 151 152 static int led_state = 0; 153 void hal_led_toggle(void){ 154 led_state = 1 - led_state; 155 printf("LED State %u\n", led_state); 156 } 157 158 static void local_version_information_handler(uint8_t * packet){ 159 printf("Local version information:\n"); 160 uint16_t hci_version = packet[6]; 161 uint16_t hci_revision = little_endian_read_16(packet, 7); 162 uint16_t lmp_version = packet[9]; 163 uint16_t manufacturer = little_endian_read_16(packet, 10); 164 uint16_t lmp_subversion = little_endian_read_16(packet, 12); 165 printf("- HCI Version 0x%04x\n", hci_version); 166 printf("- HCI Revision 0x%04x\n", hci_revision); 167 printf("- LMP Version 0x%04x\n", lmp_version); 168 printf("- LMP Subversion 0x%04x\n", lmp_subversion); 169 printf("- Manufacturer 0x%04x\n", manufacturer); 170 } 171 172 static char short_options[] = "hu:l:r"; 173 174 static struct option long_options[] = { 175 {"help", no_argument, NULL, 'h'}, 176 {"logfile", required_argument, NULL, 'l'}, 177 {"reset-tlv", no_argument, NULL, 'r'}, 178 {"node", required_argument, NULL, 'u'}, 179 {0, 0, 0, 0} 180 }; 181 182 static char *help_options[] = { 183 "print (this) help.", 184 "set file to store debug output and HCI trace.", 185 "reset bonding information stored in TLV.", 186 "set name of netgraph HCI node. Default: ubt0hci", 187 }; 188 189 static char *option_arg_name[] = { 190 "", 191 "LOGFILE", 192 "", 193 "NODE", 194 }; 195 196 static void usage(const char *name){ 197 unsigned int i; 198 printf( "usage:\n\t%s [options]\n", name ); 199 printf("valid options:\n"); 200 for( i=0; long_options[i].name != 0; i++) { 201 printf("--%-10s| -%c %-10s\t\t%s\n", long_options[i].name, long_options[i].val, option_arg_name[i], help_options[i] ); 202 } 203 } 204 205 int main(int argc, const char * argv[]){ 206 207 const char * log_file_path = NULL; 208 209 // set default device path 210 config.device_name = "ubt0hci"; 211 212 // parse command line parameters 213 while(true){ 214 int c = getopt_long( argc, (char* const *)argv, short_options, long_options, NULL ); 215 if (c < 0) { 216 break; 217 } 218 if (c == '?'){ 219 break; 220 } 221 switch (c) { 222 case 'l': 223 log_file_path = optarg; 224 break; 225 case 'u': 226 config.device_name = optarg; 227 break; 228 case 'r': 229 tlv_reset = true; 230 break; 231 case 'h': 232 default: 233 usage(argv[0]); 234 return EXIT_FAILURE; 235 } 236 } 237 /// GET STARTED with BTstack /// 238 btstack_memory_init(); 239 btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 240 241 // log into file using HCI_DUMP_PACKETLOGGER format 242 if (log_file_path == NULL){ 243 log_file_path = "/tmp/hci_dump.pklg"; 244 } 245 246 hci_dump_posix_fs_open(log_file_path, HCI_DUMP_PACKETLOGGER); 247 const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance(); 248 hci_dump_init(hci_dump_impl); 249 printf("Packet Log: %s\n", log_file_path); 250 251 // init HCI 252 const hci_transport_t * transport = hci_transport_netgraph_instance(); 253 config.type = HCI_TRANSPORT_CONFIG_USB; 254 hci_init(transport, &config); 255 256 #ifdef HAVE_PORTAUDIO 257 btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance()); 258 btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance()); 259 #endif 260 261 // inform about BTstack state 262 hci_event_callback_registration.callback = &packet_handler; 263 hci_add_event_handler(&hci_event_callback_registration); 264 265 // register callback for CTRL-c 266 btstack_signal_register_callback(SIGINT, &trigger_shutdown); 267 268 // setup app 269 btstack_main(argc, argv); 270 271 // go 272 btstack_run_loop_execute(); 273 274 return 0; 275 } 276