xref: /btstack/port/libusb/main.c (revision d39264f239eb026fd486651e238a9ef65f8504ab)
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 
468caefee3SMatthias Ringwald #include <stdint.h>
478caefee3SMatthias Ringwald #include <stdio.h>
488caefee3SMatthias Ringwald #include <stdlib.h>
498caefee3SMatthias Ringwald #include <string.h>
50dbc40e82SMatthias Ringwald #include <signal.h>
518caefee3SMatthias Ringwald 
527907f069SMatthias Ringwald #include "btstack_config.h"
538caefee3SMatthias Ringwald 
54f11fd9a9SMatthias Ringwald #include "ble/le_device_db_tlv.h"
55f9563b89SMatthias Ringwald #include "bluetooth_company_id.h"
56f11fd9a9SMatthias Ringwald #include "btstack_audio.h"
57df9a25acSMatthias Ringwald #include "btstack_chipset_realtek.h"
58f11fd9a9SMatthias Ringwald #include "btstack_chipset_zephyr.h"
59a98592bcSMatthias Ringwald #include "btstack_debug.h"
60d356a6daSMatthias Ringwald #include "btstack_event.h"
61a98592bcSMatthias Ringwald #include "btstack_memory.h"
6282636622SMatthias Ringwald #include "btstack_run_loop.h"
63b7d596c1SMatthias Ringwald #include "btstack_run_loop_posix.h"
64f11fd9a9SMatthias Ringwald #include "btstack_signal.h"
65f11fd9a9SMatthias Ringwald #include "btstack_stdin.h"
66f11fd9a9SMatthias Ringwald #include "btstack_tlv_posix.h"
67f11fd9a9SMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h"
688caefee3SMatthias Ringwald #include "hal_led.h"
698caefee3SMatthias Ringwald #include "hci.h"
708caefee3SMatthias Ringwald #include "hci_dump.h"
717435ec7bSMatthias Ringwald #include "hci_dump_posix_fs.h"
72c8dfe071SMatthias Ringwald #include "hci_transport.h"
73c8dfe071SMatthias Ringwald #include "hci_transport_usb.h"
7459bdfe96SMatthias Ringwald 
7559bdfe96SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
7659bdfe96SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv"
7759bdfe96SMatthias Ringwald static char tlv_db_path[100];
7859bdfe96SMatthias Ringwald static const btstack_tlv_t * tlv_impl;
7959bdfe96SMatthias Ringwald static btstack_tlv_posix_t   tlv_context;
8059bdfe96SMatthias Ringwald static bd_addr_t             local_addr;
818caefee3SMatthias Ringwald 
828caefee3SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
838caefee3SMatthias Ringwald 
84f9563b89SMatthias Ringwald static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc };
85f9563b89SMatthias Ringwald 
86f9563b89SMatthias Ringwald static bd_addr_t static_address;
87f9563b89SMatthias Ringwald static int using_static_address;
88f9563b89SMatthias Ringwald 
89d356a6daSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
90d356a6daSMatthias Ringwald 
91f11fd9a9SMatthias Ringwald // shutdown
92f11fd9a9SMatthias Ringwald static bool shutdown_triggered;
93f11fd9a9SMatthias Ringwald 
94f9563b89SMatthias Ringwald static void local_version_information_handler(uint8_t * packet){
95f9563b89SMatthias Ringwald     printf("Local version information:\n");
96f9563b89SMatthias Ringwald     uint16_t hci_version    = packet[6];
97f9563b89SMatthias Ringwald     uint16_t hci_revision   = little_endian_read_16(packet, 7);
98f9563b89SMatthias Ringwald     uint16_t lmp_version    = packet[9];
99f9563b89SMatthias Ringwald     uint16_t manufacturer   = little_endian_read_16(packet, 10);
100f9563b89SMatthias Ringwald     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
101f9563b89SMatthias Ringwald     printf("- HCI Version    0x%04x\n", hci_version);
102f9563b89SMatthias Ringwald     printf("- HCI Revision   0x%04x\n", hci_revision);
103f9563b89SMatthias Ringwald     printf("- LMP Version    0x%04x\n", lmp_version);
104f9563b89SMatthias Ringwald     printf("- LMP Subversion 0x%04x\n", lmp_subversion);
105f9563b89SMatthias Ringwald     printf("- Manufacturer 0x%04x\n", manufacturer);
106f9563b89SMatthias Ringwald     switch (manufacturer){
107f9563b89SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_THE_LINUX_FOUNDATION:
108f4ab4581SMatthias Ringwald             printf("- Linux Foundation - assume Zephyr hci_usb firmware running on nRF52xx\n");
109f4ab4581SMatthias Ringwald             // setup Zephyr chipset support
110f9563b89SMatthias Ringwald             hci_set_chipset(btstack_chipset_zephyr_instance());
111f4ab4581SMatthias Ringwald             // sm required to setup static random Bluetooth address
112f4ab4581SMatthias Ringwald             sm_init();
113f9563b89SMatthias Ringwald             break;
114df9a25acSMatthias Ringwald         case BLUETOOTH_COMPANY_ID_REALTEK_SEMICONDUCTOR_CORPORATION:
115df9a25acSMatthias Ringwald             printf("- Realtek controller - provide firmware and config\n");
116df9a25acSMatthias Ringwald             btstack_chipset_realtek_set_lmp_subversion(lmp_subversion);
117df9a25acSMatthias Ringwald             hci_set_chipset(btstack_chipset_realtek_instance());
118df9a25acSMatthias Ringwald             break;
119f9563b89SMatthias Ringwald         default:
120f9563b89SMatthias Ringwald             break;
121f9563b89SMatthias Ringwald     }
122f9563b89SMatthias Ringwald }
123f9563b89SMatthias Ringwald 
124d356a6daSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1259ec2630cSMatthias Ringwald     UNUSED(channel);
1269ec2630cSMatthias Ringwald     UNUSED(size);
12724a31dc7SMatthias Ringwald     uint8_t i;
12824a31dc7SMatthias Ringwald     uint8_t usb_path_len;
12924a31dc7SMatthias Ringwald     const uint8_t * usb_path;
130df9a25acSMatthias Ringwald     uint16_t product_id;
13124a31dc7SMatthias Ringwald 
132d356a6daSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
13324a31dc7SMatthias Ringwald 
13459bdfe96SMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
13524a31dc7SMatthias Ringwald         case HCI_EVENT_TRANSPORT_USB_INFO:
13624a31dc7SMatthias Ringwald             usb_path_len = hci_event_transport_usb_info_get_path_len(packet);
13724a31dc7SMatthias Ringwald             usb_path = hci_event_transport_usb_info_get_path(packet);
13824a31dc7SMatthias Ringwald             // print device path
139df9a25acSMatthias Ringwald             product_id = hci_event_transport_usb_info_get_product_id(packet);
14024a31dc7SMatthias Ringwald             printf("USB device 0x%04x/0x%04x, path: ",
141df9a25acSMatthias Ringwald                    hci_event_transport_usb_info_get_vendor_id(packet), product_id);
14224a31dc7SMatthias Ringwald             for (i=0;i<usb_path_len;i++){
14324a31dc7SMatthias Ringwald                 if (i) printf("-");
14424a31dc7SMatthias Ringwald                 printf("%02x", usb_path[i]);
14524a31dc7SMatthias Ringwald             }
14624a31dc7SMatthias Ringwald             printf("\n");
147df9a25acSMatthias Ringwald             // set Product ID for Realtek Controllers
148df9a25acSMatthias Ringwald             btstack_chipset_realtek_set_product_id(product_id);
14924a31dc7SMatthias Ringwald             break;
15059bdfe96SMatthias Ringwald         case BTSTACK_EVENT_STATE:
151d7b77a0cSMatthias Ringwald             switch (btstack_event_state_get_state(packet)){
152d7b77a0cSMatthias Ringwald                 case HCI_STATE_WORKING:
15359bdfe96SMatthias Ringwald                     gap_local_bd_addr(local_addr);
154f9563b89SMatthias Ringwald                     if (using_static_address){
155f9563b89SMatthias Ringwald                         memcpy(local_addr, static_address, 6);
156f9563b89SMatthias Ringwald                     }
15759bdfe96SMatthias Ringwald                     printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
15854736c11SMatthias Ringwald                     btstack_strcpy(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_PREFIX);
15954736c11SMatthias Ringwald                     btstack_strcat(tlv_db_path, sizeof(tlv_db_path), bd_addr_to_str_with_delimiter(local_addr, '-'));
16054736c11SMatthias Ringwald                     btstack_strcat(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_POSTFIX);
16159bdfe96SMatthias Ringwald                     tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
16259bdfe96SMatthias Ringwald                     btstack_tlv_set_instance(tlv_impl, &tlv_context);
16381862996SMatthias Ringwald #ifdef ENABLE_CLASSIC
16481862996SMatthias Ringwald                     hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
16581862996SMatthias Ringwald #endif
16681862996SMatthias Ringwald #ifdef ENABLE_BLE
16781862996SMatthias Ringwald                     le_device_db_tlv_configure(tlv_impl, &tlv_context);
16881862996SMatthias Ringwald #endif
16959bdfe96SMatthias Ringwald                     break;
170d7b77a0cSMatthias Ringwald                 case HCI_STATE_OFF:
171d7b77a0cSMatthias Ringwald                     btstack_tlv_posix_deinit(&tlv_context);
172f11fd9a9SMatthias Ringwald                     if (!shutdown_triggered) break;
173f11fd9a9SMatthias Ringwald                     // reset stdin
174f11fd9a9SMatthias Ringwald                     btstack_stdin_reset();
175f11fd9a9SMatthias Ringwald                     log_info("Good bye, see you.\n");
176f11fd9a9SMatthias Ringwald                     exit(0);
177d7b77a0cSMatthias Ringwald                     break;
178d7b77a0cSMatthias Ringwald                 default:
179d7b77a0cSMatthias Ringwald                     break;
180d7b77a0cSMatthias Ringwald             }
181d7b77a0cSMatthias Ringwald             break;
182f9563b89SMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
183*d39264f2SMatthias Ringwald             if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){
184f9563b89SMatthias Ringwald                 local_version_information_handler(packet);
185f9563b89SMatthias Ringwald             }
186f9563b89SMatthias Ringwald             if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0){
187f9563b89SMatthias Ringwald                 reverse_48(&packet[7], static_address);
188f9563b89SMatthias Ringwald                 gap_random_address_set(static_address);
189f9563b89SMatthias Ringwald                 using_static_address = 1;
190f9563b89SMatthias Ringwald             }
191f9563b89SMatthias Ringwald             break;
19259bdfe96SMatthias Ringwald         default:
19359bdfe96SMatthias Ringwald             break;
19459bdfe96SMatthias Ringwald     }
195d356a6daSMatthias Ringwald }
196d356a6daSMatthias Ringwald 
197f11fd9a9SMatthias Ringwald static void trigger_shutdown(void){
198b96c8f44SMatthias Ringwald     printf("CTRL-C - SIGINT received, shutting down..\n");
199b96c8f44SMatthias Ringwald     log_info("sigint_handler: shutting down");
200f11fd9a9SMatthias Ringwald     shutdown_triggered = true;
2018caefee3SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
2028caefee3SMatthias Ringwald }
2038caefee3SMatthias Ringwald 
2048caefee3SMatthias Ringwald static int led_state = 0;
2058caefee3SMatthias Ringwald void hal_led_toggle(void){
2068caefee3SMatthias Ringwald     led_state = 1 - led_state;
2078caefee3SMatthias Ringwald     printf("LED State %u\n", led_state);
2088caefee3SMatthias Ringwald }
2098caefee3SMatthias Ringwald 
210e13e7f54SMatthias Ringwald #define USB_MAX_PATH_LEN 7
2118caefee3SMatthias Ringwald int main(int argc, const char * argv[]){
2128caefee3SMatthias Ringwald 
213e13e7f54SMatthias Ringwald     uint8_t usb_path[USB_MAX_PATH_LEN];
214e13e7f54SMatthias Ringwald     int usb_path_len = 0;
215fb34235aSMatthias Ringwald     const char * usb_path_string = NULL;
216e13e7f54SMatthias Ringwald     if (argc >= 3 && strcmp(argv[1], "-u") == 0){
217e13e7f54SMatthias Ringwald         // parse command line options for "-u 11:22:33"
218fb34235aSMatthias Ringwald         usb_path_string = argv[2];
219e13e7f54SMatthias Ringwald         printf("Specified USB Path: ");
220e13e7f54SMatthias Ringwald         while (1){
221e13e7f54SMatthias Ringwald             char * delimiter;
222fb34235aSMatthias Ringwald             int port = strtol(usb_path_string, &delimiter, 16);
223e13e7f54SMatthias Ringwald             usb_path[usb_path_len] = port;
224e13e7f54SMatthias Ringwald             usb_path_len++;
225e13e7f54SMatthias Ringwald             printf("%02x ", port);
226e13e7f54SMatthias Ringwald             if (!delimiter) break;
227a4c4a5d6SMatthias Ringwald             if (*delimiter != ':' && *delimiter != '-') break;
228fb34235aSMatthias Ringwald             usb_path_string = delimiter+1;
229e13e7f54SMatthias Ringwald         }
230e13e7f54SMatthias Ringwald         printf("\n");
2317c3351a5SMatthias Ringwald         argc -= 2;
2320033ffe4SMatthias Ringwald         memmove((void *) &argv[1], &argv[3], (argc-1) * sizeof(char *));
233e13e7f54SMatthias Ringwald     }
234e13e7f54SMatthias Ringwald 
2358caefee3SMatthias Ringwald 	/// GET STARTED with BTstack ///
2368caefee3SMatthias Ringwald 	btstack_memory_init();
237528a4a3bSMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
2388caefee3SMatthias Ringwald 
239e13e7f54SMatthias Ringwald     if (usb_path_len){
240e13e7f54SMatthias Ringwald         hci_transport_usb_set_path(usb_path_len, usb_path);
241e13e7f54SMatthias Ringwald     }
242e13e7f54SMatthias Ringwald 
2437435ec7bSMatthias Ringwald     // log into file using HCI_DUMP_PACKETLOGGER format
24402065453SMatthias Ringwald     char pklg_path[100];
24554736c11SMatthias Ringwald     btstack_strcpy(pklg_path, sizeof(pklg_path),  "/tmp/hci_dump");
24602065453SMatthias Ringwald     if (usb_path_len){
24754736c11SMatthias Ringwald         btstack_strcat(pklg_path, sizeof(pklg_path),  "_");
24854736c11SMatthias Ringwald         btstack_strcat(pklg_path, sizeof(pklg_path),  usb_path_string);
24902065453SMatthias Ringwald     }
25054736c11SMatthias Ringwald     btstack_strcat(pklg_path, sizeof(pklg_path), ".pklg");
2517435ec7bSMatthias Ringwald     hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER);
2527435ec7bSMatthias Ringwald     const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance();
2537435ec7bSMatthias Ringwald     hci_dump_init(hci_dump_impl);
25402065453SMatthias Ringwald     printf("Packet Log: %s\n", pklg_path);
2558caefee3SMatthias Ringwald 
2568caefee3SMatthias Ringwald     // init HCI
2572d5e09d6SMatthias Ringwald 	hci_init(hci_transport_usb_instance(), NULL);
25835454696SMatthias Ringwald 
259e739af98SMatthias Ringwald #ifdef HAVE_PORTAUDIO
2603d3351f3SMatthias Ringwald     btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance());
2613d3351f3SMatthias Ringwald     btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance());
262e739af98SMatthias Ringwald #endif
263e739af98SMatthias Ringwald 
264d356a6daSMatthias Ringwald     // inform about BTstack state
265d356a6daSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
266d356a6daSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
267d356a6daSMatthias Ringwald 
268f11fd9a9SMatthias Ringwald     // register callback for CTRL-c
269f11fd9a9SMatthias Ringwald     btstack_signal_register_callback(SIGINT, &trigger_shutdown);
2708caefee3SMatthias Ringwald 
271df9a25acSMatthias Ringwald     // register known Realtek USB Controllers
272df9a25acSMatthias Ringwald     uint16_t realtek_num_controllers = btstack_chipset_realtek_get_num_usb_controllers();
273df9a25acSMatthias Ringwald     uint16_t i;
274df9a25acSMatthias Ringwald     for (i=0;i<realtek_num_controllers;i++){
275df9a25acSMatthias Ringwald         uint16_t vendor_id;
276df9a25acSMatthias Ringwald         uint16_t product_id;
277df9a25acSMatthias Ringwald         btstack_chipset_realtek_get_vendor_product_id(i, &vendor_id, &product_id);
278df9a25acSMatthias Ringwald         hci_transport_usb_add_device(vendor_id, product_id);
279df9a25acSMatthias Ringwald     }
280df9a25acSMatthias Ringwald 
2818caefee3SMatthias Ringwald     // setup app
2828caefee3SMatthias Ringwald     btstack_main(argc, argv);
2838caefee3SMatthias Ringwald 
2848caefee3SMatthias Ringwald     // go
285528a4a3bSMatthias Ringwald     btstack_run_loop_execute();
2868caefee3SMatthias Ringwald 
2878caefee3SMatthias Ringwald     return 0;
2888caefee3SMatthias Ringwald }
289