xref: /btstack/port/libusb/main.c (revision c1dc38453e5a3a6c46c40f0040210faa26c5d8f4)
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
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH 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 
38f11fd9a9SMatthias 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 
460ad02723SMatthias Ringwald #include <getopt.h>
478caefee3SMatthias Ringwald #include <stdint.h>
488caefee3SMatthias Ringwald #include <stdio.h>
498caefee3SMatthias Ringwald #include <stdlib.h>
508caefee3SMatthias Ringwald #include <string.h>
51dbc40e82SMatthias Ringwald #include <signal.h>
528fb9c30cSMatthias Ringwald #include <unistd.h>
539d506991SMatthias Ringwald #include <hci_dump_posix_stdout.h>
548caefee3SMatthias Ringwald 
557907f069SMatthias Ringwald #include "btstack_config.h"
568caefee3SMatthias Ringwald 
57f11fd9a9SMatthias Ringwald #include "ble/le_device_db_tlv.h"
58f9563b89SMatthias Ringwald #include "bluetooth_company_id.h"
59f11fd9a9SMatthias Ringwald #include "btstack_audio.h"
60df9a25acSMatthias Ringwald #include "btstack_chipset_realtek.h"
61f11fd9a9SMatthias Ringwald #include "btstack_chipset_zephyr.h"
62a98592bcSMatthias Ringwald #include "btstack_debug.h"
63d356a6daSMatthias Ringwald #include "btstack_event.h"
64a98592bcSMatthias Ringwald #include "btstack_memory.h"
6582636622SMatthias Ringwald #include "btstack_run_loop.h"
66b7d596c1SMatthias Ringwald #include "btstack_run_loop_posix.h"
67f11fd9a9SMatthias Ringwald #include "btstack_signal.h"
68f11fd9a9SMatthias Ringwald #include "btstack_stdin.h"
69f11fd9a9SMatthias Ringwald #include "btstack_tlv_posix.h"
70f11fd9a9SMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h"
718caefee3SMatthias Ringwald #include "hal_led.h"
728caefee3SMatthias Ringwald #include "hci.h"
738caefee3SMatthias Ringwald #include "hci_dump.h"
747435ec7bSMatthias Ringwald #include "hci_dump_posix_fs.h"
75c8dfe071SMatthias Ringwald #include "hci_transport.h"
76c8dfe071SMatthias Ringwald #include "hci_transport_usb.h"
7759bdfe96SMatthias Ringwald 
78*c1dc3845SMatthias Ringwald #define USB_VENDOR_ID_REALTEK 0x0bda
79*c1dc3845SMatthias Ringwald 
8059bdfe96SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
8159bdfe96SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv"
8259bdfe96SMatthias Ringwald static char tlv_db_path[100];
839faf05e8SMatthias Ringwald static bool tlv_reset;
8459bdfe96SMatthias Ringwald static const btstack_tlv_t * tlv_impl;
8559bdfe96SMatthias Ringwald static btstack_tlv_posix_t   tlv_context;
8659bdfe96SMatthias Ringwald static bd_addr_t             local_addr;
878caefee3SMatthias Ringwald 
888caefee3SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
898caefee3SMatthias Ringwald 
90f9563b89SMatthias Ringwald static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc };
91f9563b89SMatthias Ringwald 
92f9563b89SMatthias Ringwald static bd_addr_t static_address;
93f9563b89SMatthias Ringwald static int using_static_address;
94f9563b89SMatthias Ringwald 
95d356a6daSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
96d356a6daSMatthias Ringwald 
97f11fd9a9SMatthias Ringwald // shutdown
98f11fd9a9SMatthias Ringwald static bool shutdown_triggered;
99f11fd9a9SMatthias Ringwald 
100f9563b89SMatthias Ringwald static void local_version_information_handler(uint8_t * packet){
101f9563b89SMatthias Ringwald     printf("Local version information:\n");
102f9563b89SMatthias Ringwald     uint16_t hci_version    = packet[6];
103f9563b89SMatthias Ringwald     uint16_t hci_revision   = little_endian_read_16(packet, 7);
104f9563b89SMatthias Ringwald     uint16_t lmp_version    = packet[9];
105f9563b89SMatthias Ringwald     uint16_t manufacturer   = little_endian_read_16(packet, 10);
106f9563b89SMatthias Ringwald     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
107f9563b89SMatthias Ringwald     printf("- HCI Version    0x%04x\n", hci_version);
108f9563b89SMatthias Ringwald     printf("- HCI Revision   0x%04x\n", hci_revision);
109f9563b89SMatthias Ringwald     printf("- LMP Version    0x%04x\n", lmp_version);
110f9563b89SMatthias Ringwald     printf("- LMP Subversion 0x%04x\n", lmp_subversion);
111f9563b89SMatthias Ringwald     printf("- Manufacturer   0x%04x\n", manufacturer);
112f9563b89SMatthias Ringwald     switch (manufacturer){
113f9563b89SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_THE_LINUX_FOUNDATION:
114f4ab4581SMatthias Ringwald             printf("- Linux Foundation - assume Zephyr hci_usb firmware running on nRF52xx\n");
115f4ab4581SMatthias Ringwald             // setup Zephyr chipset support
116f9563b89SMatthias Ringwald             hci_set_chipset(btstack_chipset_zephyr_instance());
117f4ab4581SMatthias Ringwald             // sm required to setup static random Bluetooth address
118f4ab4581SMatthias Ringwald             sm_init();
119f9563b89SMatthias Ringwald             break;
120f9563b89SMatthias Ringwald         default:
121f9563b89SMatthias Ringwald             break;
122f9563b89SMatthias Ringwald     }
123f9563b89SMatthias Ringwald }
124f9563b89SMatthias Ringwald 
125d356a6daSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1269ec2630cSMatthias Ringwald     UNUSED(channel);
1279ec2630cSMatthias Ringwald     UNUSED(size);
12824a31dc7SMatthias Ringwald     uint8_t i;
12924a31dc7SMatthias Ringwald     uint8_t usb_path_len;
13024a31dc7SMatthias Ringwald     const uint8_t * usb_path;
131df9a25acSMatthias Ringwald     uint16_t product_id;
132*c1dc3845SMatthias Ringwald     uint16_t vendor_id;
13324a31dc7SMatthias Ringwald 
134d356a6daSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
13524a31dc7SMatthias Ringwald 
13659bdfe96SMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
13724a31dc7SMatthias Ringwald         case HCI_EVENT_TRANSPORT_USB_INFO:
13824a31dc7SMatthias Ringwald             usb_path_len = hci_event_transport_usb_info_get_path_len(packet);
13924a31dc7SMatthias Ringwald             usb_path = hci_event_transport_usb_info_get_path(packet);
14024a31dc7SMatthias Ringwald             // print device path
141df9a25acSMatthias Ringwald             product_id = hci_event_transport_usb_info_get_product_id(packet);
142*c1dc3845SMatthias Ringwald             vendor_id = hci_event_transport_usb_info_get_vendor_id(packet);
143*c1dc3845SMatthias Ringwald             printf("USB device 0x%04x/0x%04x, path: ", vendor_id, product_id);
14424a31dc7SMatthias Ringwald             for (i=0;i<usb_path_len;i++){
14524a31dc7SMatthias Ringwald                 if (i) printf("-");
14624a31dc7SMatthias Ringwald                 printf("%02x", usb_path[i]);
14724a31dc7SMatthias Ringwald             }
14824a31dc7SMatthias Ringwald             printf("\n");
149*c1dc3845SMatthias Ringwald 
150*c1dc3845SMatthias Ringwald             // set Product ID for Realtek Controllers and use Realtek-specific stack startup
151*c1dc3845SMatthias Ringwald             if (vendor_id == USB_VENDOR_ID_REALTEK) {
152*c1dc3845SMatthias Ringwald                 printf("Realtek Controller - requires firmware and config download\n");
153df9a25acSMatthias Ringwald                 btstack_chipset_realtek_set_product_id(product_id);
154*c1dc3845SMatthias Ringwald                 hci_set_chipset(btstack_chipset_realtek_instance());
155*c1dc3845SMatthias Ringwald                 hci_enable_custom_pre_init();
156*c1dc3845SMatthias Ringwald             }
15724a31dc7SMatthias Ringwald             break;
15859bdfe96SMatthias Ringwald         case BTSTACK_EVENT_STATE:
159d7b77a0cSMatthias Ringwald             switch (btstack_event_state_get_state(packet)){
160d7b77a0cSMatthias Ringwald                 case HCI_STATE_WORKING:
16159bdfe96SMatthias Ringwald                     gap_local_bd_addr(local_addr);
162f9563b89SMatthias Ringwald                     if (using_static_address){
163f9563b89SMatthias Ringwald                         memcpy(local_addr, static_address, 6);
164f9563b89SMatthias Ringwald                     }
16554736c11SMatthias Ringwald                     btstack_strcpy(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_PREFIX);
16654736c11SMatthias Ringwald                     btstack_strcat(tlv_db_path, sizeof(tlv_db_path), bd_addr_to_str_with_delimiter(local_addr, '-'));
16754736c11SMatthias Ringwald                     btstack_strcat(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_POSTFIX);
1689faf05e8SMatthias Ringwald                     printf("TLV path: %s", tlv_db_path);
1699faf05e8SMatthias Ringwald                     if (tlv_reset){
1709faf05e8SMatthias Ringwald                         int rc = unlink(tlv_db_path);
1719faf05e8SMatthias Ringwald                         if (rc == 0) {
1729faf05e8SMatthias Ringwald                             printf(", reset ok");
1739faf05e8SMatthias Ringwald                         } else {
1749faf05e8SMatthias Ringwald                             printf(", reset failed with result = %d", rc);
1759faf05e8SMatthias Ringwald                         }
1769faf05e8SMatthias Ringwald                     }
1779faf05e8SMatthias Ringwald                     printf("\n");
17859bdfe96SMatthias Ringwald                     tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
17959bdfe96SMatthias Ringwald                     btstack_tlv_set_instance(tlv_impl, &tlv_context);
18081862996SMatthias Ringwald #ifdef ENABLE_CLASSIC
18181862996SMatthias Ringwald                     hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
18281862996SMatthias Ringwald #endif
18381862996SMatthias Ringwald #ifdef ENABLE_BLE
18481862996SMatthias Ringwald                     le_device_db_tlv_configure(tlv_impl, &tlv_context);
18581862996SMatthias Ringwald #endif
1869faf05e8SMatthias Ringwald                     printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
18759bdfe96SMatthias Ringwald                     break;
188d7b77a0cSMatthias Ringwald                 case HCI_STATE_OFF:
189d7b77a0cSMatthias Ringwald                     btstack_tlv_posix_deinit(&tlv_context);
190f11fd9a9SMatthias Ringwald                     if (!shutdown_triggered) break;
191f11fd9a9SMatthias Ringwald                     // reset stdin
192f11fd9a9SMatthias Ringwald                     btstack_stdin_reset();
193f11fd9a9SMatthias Ringwald                     log_info("Good bye, see you.\n");
194f11fd9a9SMatthias Ringwald                     exit(0);
195d7b77a0cSMatthias Ringwald                     break;
196d7b77a0cSMatthias Ringwald                 default:
197d7b77a0cSMatthias Ringwald                     break;
198d7b77a0cSMatthias Ringwald             }
199d7b77a0cSMatthias Ringwald             break;
200f9563b89SMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
201d39264f2SMatthias Ringwald             if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){
202f9563b89SMatthias Ringwald                 local_version_information_handler(packet);
203f9563b89SMatthias Ringwald             }
204f9563b89SMatthias Ringwald             if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0){
205f9563b89SMatthias Ringwald                 reverse_48(&packet[7], static_address);
206f9563b89SMatthias Ringwald                 gap_random_address_set(static_address);
207f9563b89SMatthias Ringwald                 using_static_address = 1;
208f9563b89SMatthias Ringwald             }
209f9563b89SMatthias Ringwald             break;
21059bdfe96SMatthias Ringwald         default:
21159bdfe96SMatthias Ringwald             break;
21259bdfe96SMatthias Ringwald     }
213d356a6daSMatthias Ringwald }
214d356a6daSMatthias Ringwald 
215f11fd9a9SMatthias Ringwald static void trigger_shutdown(void){
216b96c8f44SMatthias Ringwald     printf("CTRL-C - SIGINT received, shutting down..\n");
217b96c8f44SMatthias Ringwald     log_info("sigint_handler: shutting down");
218f11fd9a9SMatthias Ringwald     shutdown_triggered = true;
2198caefee3SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
2208caefee3SMatthias Ringwald }
2218caefee3SMatthias Ringwald 
2228caefee3SMatthias Ringwald static int led_state = 0;
2230ad02723SMatthias Ringwald 
2248caefee3SMatthias Ringwald void hal_led_toggle(void){
2258caefee3SMatthias Ringwald     led_state = 1 - led_state;
2268caefee3SMatthias Ringwald     printf("LED State %u\n", led_state);
2278caefee3SMatthias Ringwald }
2288caefee3SMatthias Ringwald 
2299faf05e8SMatthias Ringwald static char short_options[] = "hu:l:r";
2300ad02723SMatthias Ringwald 
2310ad02723SMatthias Ringwald static struct option long_options[] = {
2320ad02723SMatthias Ringwald     {"help",        no_argument,        NULL,   'h'},
233834e96b3SMatthias Ringwald     {"logfile",    required_argument,  NULL,   'l'},
2349faf05e8SMatthias Ringwald     {"reset-tlv",    no_argument,       NULL,   'r'},
2350ad02723SMatthias Ringwald     {"usbpath",    required_argument,  NULL,   'u'},
2360ad02723SMatthias Ringwald     {0, 0, 0, 0}
2370ad02723SMatthias Ringwald };
2380ad02723SMatthias Ringwald 
2390ad02723SMatthias Ringwald static char *help_options[] = {
2400ad02723SMatthias Ringwald     "print (this) help.",
241834e96b3SMatthias Ringwald     "set file to store debug output and HCI trace.",
2429faf05e8SMatthias Ringwald     "reset bonding information stored in TLV.",
2430ad02723SMatthias Ringwald     "set USB path to Bluetooth Controller.",
2440ad02723SMatthias Ringwald };
2450ad02723SMatthias Ringwald 
2460ad02723SMatthias Ringwald static char *option_arg_name[] = {
2470ad02723SMatthias Ringwald     "",
248834e96b3SMatthias Ringwald     "LOGFILE",
2499faf05e8SMatthias Ringwald     "",
2500ad02723SMatthias Ringwald     "USBPATH",
2510ad02723SMatthias Ringwald };
2520ad02723SMatthias Ringwald 
2530ad02723SMatthias Ringwald static void usage(const char *name){
2540ad02723SMatthias Ringwald     unsigned int i;
2550ad02723SMatthias Ringwald     printf( "usage:\n\t%s [options]\n", name );
2560ad02723SMatthias Ringwald     printf("valid options:\n");
2570ad02723SMatthias Ringwald     for( i=0; long_options[i].name != 0; i++) {
2580ad02723SMatthias Ringwald         printf("--%-10s| -%c  %-10s\t\t%s\n", long_options[i].name, long_options[i].val, option_arg_name[i], help_options[i] );
2590ad02723SMatthias Ringwald     }
2600ad02723SMatthias Ringwald }
2610ad02723SMatthias Ringwald 
262e13e7f54SMatthias Ringwald #define USB_MAX_PATH_LEN 7
2638caefee3SMatthias Ringwald int main(int argc, const char * argv[]){
2648caefee3SMatthias Ringwald 
265e13e7f54SMatthias Ringwald     uint8_t usb_path[USB_MAX_PATH_LEN];
266e13e7f54SMatthias Ringwald     int usb_path_len = 0;
267fb34235aSMatthias Ringwald     const char * usb_path_string = NULL;
268834e96b3SMatthias Ringwald     const char * log_file_path = NULL;
2690ad02723SMatthias Ringwald 
2700ad02723SMatthias Ringwald     // parse command line parameters
2710ad02723SMatthias Ringwald     while(true){
2720ad02723SMatthias Ringwald         int c = getopt_long( argc, (char* const *)argv, short_options, long_options, NULL );
2730ad02723SMatthias Ringwald         if (c < 0) {
2740ad02723SMatthias Ringwald             break;
2750ad02723SMatthias Ringwald         }
27697359979SMatthias Ringwald         if (c == '?'){
27797359979SMatthias Ringwald             break;
27897359979SMatthias Ringwald         }
2790ad02723SMatthias Ringwald         switch (c) {
2800ad02723SMatthias Ringwald             case 'u':
281834e96b3SMatthias Ringwald                 usb_path_string = optarg;
282834e96b3SMatthias Ringwald                 break;
283834e96b3SMatthias Ringwald             case 'l':
284834e96b3SMatthias Ringwald                 log_file_path = optarg;
2850ad02723SMatthias Ringwald                 break;
2869faf05e8SMatthias Ringwald             case 'r':
2879faf05e8SMatthias Ringwald                 tlv_reset = true;
2889faf05e8SMatthias Ringwald                 break;
2890ad02723SMatthias Ringwald             case 'h':
2900ad02723SMatthias Ringwald             default:
2910ad02723SMatthias Ringwald                 usage(argv[0]);
2920ad02723SMatthias Ringwald                 return EXIT_FAILURE;
2930ad02723SMatthias Ringwald         }
2940ad02723SMatthias Ringwald     }
2950ad02723SMatthias Ringwald 
2960ad02723SMatthias Ringwald     if (usb_path_string != NULL){
2970ad02723SMatthias Ringwald         // parse command line options for "-u 11:22:33"
298e13e7f54SMatthias Ringwald         printf("Specified USB Path: ");
299e13e7f54SMatthias Ringwald         while (1){
300e13e7f54SMatthias Ringwald             char * delimiter;
301fb34235aSMatthias Ringwald             int port = strtol(usb_path_string, &delimiter, 16);
302e13e7f54SMatthias Ringwald             usb_path[usb_path_len] = port;
303e13e7f54SMatthias Ringwald             usb_path_len++;
304e13e7f54SMatthias Ringwald             printf("%02x ", port);
305e13e7f54SMatthias Ringwald             if (!delimiter) break;
306a4c4a5d6SMatthias Ringwald             if (*delimiter != ':' && *delimiter != '-') break;
307fb34235aSMatthias Ringwald             usb_path_string = delimiter+1;
308e13e7f54SMatthias Ringwald         }
309e13e7f54SMatthias Ringwald         printf("\n");
310e13e7f54SMatthias Ringwald     }
311e13e7f54SMatthias Ringwald 
3128caefee3SMatthias Ringwald 	/// GET STARTED with BTstack ///
3138caefee3SMatthias Ringwald 	btstack_memory_init();
314528a4a3bSMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
3158caefee3SMatthias Ringwald 
316e13e7f54SMatthias Ringwald     if (usb_path_len){
317e13e7f54SMatthias Ringwald         hci_transport_usb_set_path(usb_path_len, usb_path);
318e13e7f54SMatthias Ringwald     }
319e13e7f54SMatthias Ringwald 
3207435ec7bSMatthias Ringwald     // log into file using HCI_DUMP_PACKETLOGGER format
32102065453SMatthias Ringwald     char pklg_path[100];
322a94ee701SMatthias Ringwald     if (log_file_path == NULL){
32354736c11SMatthias Ringwald         btstack_strcpy(pklg_path, sizeof(pklg_path),  "/tmp/hci_dump");
32402065453SMatthias Ringwald         if (usb_path_len){
32554736c11SMatthias Ringwald             btstack_strcat(pklg_path, sizeof(pklg_path),  "_");
32654736c11SMatthias Ringwald             btstack_strcat(pklg_path, sizeof(pklg_path),  usb_path_string);
32702065453SMatthias Ringwald         }
32854736c11SMatthias Ringwald         btstack_strcat(pklg_path, sizeof(pklg_path), ".pklg");
329834e96b3SMatthias Ringwald         log_file_path = pklg_path;
330834e96b3SMatthias Ringwald     }
331834e96b3SMatthias Ringwald 
332834e96b3SMatthias Ringwald     hci_dump_posix_fs_open(log_file_path, HCI_DUMP_PACKETLOGGER);
3337435ec7bSMatthias Ringwald     const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance();
3347435ec7bSMatthias Ringwald     hci_dump_init(hci_dump_impl);
335834e96b3SMatthias Ringwald     printf("Packet Log: %s\n", log_file_path);
3368caefee3SMatthias Ringwald 
3378caefee3SMatthias Ringwald     // init HCI
3382d5e09d6SMatthias Ringwald 	hci_init(hci_transport_usb_instance(), NULL);
33935454696SMatthias Ringwald 
340e739af98SMatthias Ringwald #ifdef HAVE_PORTAUDIO
3413d3351f3SMatthias Ringwald     btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance());
3423d3351f3SMatthias Ringwald     btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance());
343e739af98SMatthias Ringwald #endif
344e739af98SMatthias Ringwald 
345d356a6daSMatthias Ringwald     // inform about BTstack state
346d356a6daSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
347d356a6daSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
348d356a6daSMatthias Ringwald 
349f11fd9a9SMatthias Ringwald     // register callback for CTRL-c
350f11fd9a9SMatthias Ringwald     btstack_signal_register_callback(SIGINT, &trigger_shutdown);
3518caefee3SMatthias Ringwald 
352df9a25acSMatthias Ringwald     // register known Realtek USB Controllers
353df9a25acSMatthias Ringwald     uint16_t realtek_num_controllers = btstack_chipset_realtek_get_num_usb_controllers();
354df9a25acSMatthias Ringwald     uint16_t i;
355df9a25acSMatthias Ringwald     for (i=0;i<realtek_num_controllers;i++){
356df9a25acSMatthias Ringwald         uint16_t vendor_id;
357df9a25acSMatthias Ringwald         uint16_t product_id;
358df9a25acSMatthias Ringwald         btstack_chipset_realtek_get_vendor_product_id(i, &vendor_id, &product_id);
359df9a25acSMatthias Ringwald         hci_transport_usb_add_device(vendor_id, product_id);
360df9a25acSMatthias Ringwald     }
361df9a25acSMatthias Ringwald 
3628caefee3SMatthias Ringwald     // setup app
3638caefee3SMatthias Ringwald     btstack_main(argc, argv);
3648caefee3SMatthias Ringwald 
3658caefee3SMatthias Ringwald     // go
366528a4a3bSMatthias Ringwald     btstack_run_loop_execute();
3678caefee3SMatthias Ringwald 
3688caefee3SMatthias Ringwald     return 0;
3698caefee3SMatthias Ringwald }
370