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