xref: /btstack/port/libusb/main.c (revision 31437b52049f7a1b5a54d9c71f935fe16cc0e09d)
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 
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <signal.h>
51 
52 #include "btstack_config.h"
53 
54 #include "bluetooth_company_id.h"
55 #include "btstack_debug.h"
56 #include "btstack_event.h"
57 #include "ble/le_device_db_tlv.h"
58 #include "classic/btstack_link_key_db_tlv.h"
59 #include "btstack_memory.h"
60 #include "btstack_run_loop.h"
61 #include "btstack_run_loop_posix.h"
62 #include "hal_led.h"
63 #include "hci.h"
64 #include "hci_dump.h"
65 #include "hci_dump_posix_fs.h"
66 #include "btstack_stdin.h"
67 #include "btstack_audio.h"
68 #include "btstack_tlv_posix.h"
69 #include "btstack_chipset_zephyr.h"
70 
71 #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
72 #define TLV_DB_PATH_POSTFIX ".tlv"
73 static char tlv_db_path[100];
74 static const btstack_tlv_t * tlv_impl;
75 static btstack_tlv_posix_t   tlv_context;
76 static bd_addr_t             local_addr;
77 
78 int btstack_main(int argc, const char * argv[]);
79 
80 static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc };
81 
82 static bd_addr_t static_address;
83 static int using_static_address;
84 
85 static btstack_packet_callback_registration_t hci_event_callback_registration;
86 
87 static void local_version_information_handler(uint8_t * packet){
88     printf("Local version information:\n");
89     uint16_t hci_version    = packet[6];
90     uint16_t hci_revision   = little_endian_read_16(packet, 7);
91     uint16_t lmp_version    = packet[9];
92     uint16_t manufacturer   = little_endian_read_16(packet, 10);
93     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
94     printf("- HCI Version    0x%04x\n", hci_version);
95     printf("- HCI Revision   0x%04x\n", hci_revision);
96     printf("- LMP Version    0x%04x\n", lmp_version);
97     printf("- LMP Subversion 0x%04x\n", lmp_subversion);
98     printf("- Manufacturer 0x%04x\n", manufacturer);
99     switch (manufacturer){
100         case BLUETOOTH_COMPANY_ID_THE_LINUX_FOUNDATION:
101             printf("- Linux Foundation - assume Zephyr hci_usb firmware running on nRF52xx\n");
102             // setup Zephyr chipset support
103             hci_set_chipset(btstack_chipset_zephyr_instance());
104             // sm required to setup static random Bluetooth address
105             sm_init();
106             break;
107         default:
108             break;
109     }
110 }
111 
112 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
113     UNUSED(channel);
114     UNUSED(size);
115     if (packet_type != HCI_EVENT_PACKET) return;
116     switch (hci_event_packet_get_type(packet)){
117         case BTSTACK_EVENT_STATE:
118             switch (btstack_event_state_get_state(packet)){
119                 case HCI_STATE_WORKING:
120                     gap_local_bd_addr(local_addr);
121                     if (using_static_address){
122                         memcpy(local_addr, static_address, 6);
123                     }
124                     printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
125                     strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
126                     strcat(tlv_db_path, bd_addr_to_str(local_addr));
127                     strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
128                     tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
129                     btstack_tlv_set_instance(tlv_impl, &tlv_context);
130 #ifdef ENABLE_CLASSIC
131                     hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
132 #endif
133 #ifdef ENABLE_BLE
134                     le_device_db_tlv_configure(tlv_impl, &tlv_context);
135 #endif
136                     break;
137                 case HCI_STATE_OFF:
138                     btstack_tlv_posix_deinit(&tlv_context);
139                     break;
140                 default:
141                     break;
142             }
143             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
144             break;
145         case HCI_EVENT_COMMAND_COMPLETE:
146             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){
147                 local_version_information_handler(packet);
148             }
149             if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0){
150                 reverse_48(&packet[7], static_address);
151                 gap_random_address_set(static_address);
152                 using_static_address = 1;
153             }
154             break;
155         default:
156             break;
157     }
158 }
159 
160 static void sigint_handler(int param){
161     UNUSED(param);
162 
163     printf("CTRL-C - SIGINT received, shutting down..\n");
164     log_info("sigint_handler: shutting down");
165 
166     // reset anyway
167     btstack_stdin_reset();
168 
169     // power down
170     hci_power_control(HCI_POWER_OFF);
171     hci_close();
172     log_info("Good bye, see you.\n");
173     exit(0);
174 }
175 
176 static int led_state = 0;
177 void hal_led_toggle(void){
178     led_state = 1 - led_state;
179     printf("LED State %u\n", led_state);
180 }
181 
182 #define USB_MAX_PATH_LEN 7
183 int main(int argc, const char * argv[]){
184 
185     uint8_t usb_path[USB_MAX_PATH_LEN];
186     int usb_path_len = 0;
187     const char * usb_path_string = NULL;
188     if (argc >= 3 && strcmp(argv[1], "-u") == 0){
189         // parse command line options for "-u 11:22:33"
190         usb_path_string = argv[2];
191         printf("Specified USB Path: ");
192         while (1){
193             char * delimiter;
194             int port = strtol(usb_path_string, &delimiter, 16);
195             usb_path[usb_path_len] = port;
196             usb_path_len++;
197             printf("%02x ", port);
198             if (!delimiter) break;
199             if (*delimiter != ':' && *delimiter != '-') break;
200             usb_path_string = delimiter+1;
201         }
202         printf("\n");
203         argc -= 2;
204         memmove(&argv[1], &argv[3], (argc-1) * sizeof(char *));
205     }
206 
207 	/// GET STARTED with BTstack ///
208 	btstack_memory_init();
209     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
210 
211     if (usb_path_len){
212         hci_transport_usb_set_path(usb_path_len, usb_path);
213     }
214 
215     // log into file using HCI_DUMP_PACKETLOGGER format
216     char pklg_path[100];
217     strcpy(pklg_path, "/tmp/hci_dump");
218     if (usb_path_len){
219         strcat(pklg_path, "_");
220         strcat(pklg_path, usb_path_string);
221     }
222     strcat(pklg_path, ".pklg");
223     hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER);
224     const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance();
225     hci_dump_init(hci_dump_impl);
226     printf("Packet Log: %s\n", pklg_path);
227 
228     // init HCI
229 	hci_init(hci_transport_usb_instance(), NULL);
230 
231 #ifdef HAVE_PORTAUDIO
232     btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance());
233     btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance());
234 #endif
235 
236     // inform about BTstack state
237     hci_event_callback_registration.callback = &packet_handler;
238     hci_add_event_handler(&hci_event_callback_registration);
239 
240     // handle CTRL-c
241     signal(SIGINT, sigint_handler);
242 
243     // setup app
244     btstack_main(argc, argv);
245 
246     // go
247     btstack_run_loop_execute();
248 
249     return 0;
250 }
251