xref: /btstack/port/posix-h4/main.c (revision ced70f9bfeafe291ec597a3a9cc862e39e0da3ce)
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_chipset_bcm.h"
60 #include "btstack_chipset_cc256x.h"
61 #include "btstack_chipset_csr.h"
62 #include "btstack_chipset_em9301.h"
63 #include "btstack_chipset_stlc2500d.h"
64 #include "btstack_chipset_tc3566x.h"
65 #include "btstack_debug.h"
66 #include "btstack_event.h"
67 #include "btstack_memory.h"
68 #include "btstack_run_loop.h"
69 #include "btstack_run_loop_posix.h"
70 #include "btstack_signal.h"
71 #include "btstack_stdin.h"
72 #include "btstack_tlv_posix.h"
73 #include "btstack_uart.h"
74 #include "classic/btstack_link_key_db_tlv.h"
75 #include "hci.h"
76 #include "hci_dump.h"
77 #include "hci_dump_posix_fs.h"
78 #include "hci_transport.h"
79 #include "hci_transport_h4.h"
80 
81 
82 #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
83 #define TLV_DB_PATH_POSTFIX ".tlv"
84 static char tlv_db_path[100];
85 static bool tlv_reset;
86 static const btstack_tlv_t * tlv_impl;
87 static btstack_tlv_posix_t   tlv_context;
88 static bd_addr_t             local_addr;
89 
90 static int is_bcm;
91 // shutdown
92 static bool shutdown_triggered;
93 
94 int btstack_main(int argc, const char * argv[]);
95 static void local_version_information_handler(uint8_t * packet);
96 
97 static hci_transport_config_uart_t config = {
98     HCI_TRANSPORT_CONFIG_UART,
99     115200,
100     0,  // main baudrate
101     1,  // flow control
102     NULL,
103 };
104 
105 static btstack_packet_callback_registration_t hci_event_callback_registration;
106 
107 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
108     if (packet_type != HCI_EVENT_PACKET) return;
109     switch (hci_event_packet_get_type(packet)){
110         case BTSTACK_EVENT_STATE:
111             switch(btstack_event_state_get_state(packet)){
112                 case HCI_STATE_WORKING:
113                     gap_local_bd_addr(local_addr);
114                     printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
115                     btstack_strcpy(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_PREFIX);
116                     btstack_strcat(tlv_db_path, sizeof(tlv_db_path), bd_addr_to_str_with_delimiter(local_addr, '-'));
117                     btstack_strcat(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_POSTFIX);
118                     printf("TLV path: %s", tlv_db_path);
119                     if (tlv_reset){
120                         int rc = unlink(tlv_db_path);
121                         if (rc == 0) {
122                             printf(", reset ok");
123                         } else {
124                             printf(", reset failed with result = %d", rc);
125                         }
126                     }
127                     printf("\n");
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                     if (!shutdown_triggered) break;
140                     // reset stdin
141                     btstack_stdin_reset();
142                     log_info("Good bye, see you.\n");
143                     exit(0);
144                     break;
145                 default:
146                     break;
147             }
148             break;
149         case HCI_EVENT_COMMAND_COMPLETE:
150              if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_NAME){
151                 if (hci_event_command_complete_get_return_parameters(packet)[0]) break;
152                 // terminate, name 248 chars
153                 packet[6+248] = 0;
154                 printf("Local name: %s\n", &packet[6]);
155                 if (is_bcm){
156                     btstack_chipset_bcm_set_device_name((const char *)&packet[6]);
157                 }
158             }
159             if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){
160                 local_version_information_handler(packet);
161             }
162             break;
163         default:
164             break;
165     }
166 }
167 
168 static void trigger_shutdown(void){
169     printf("CTRL-C - SIGINT received, shutting down..\n");
170     log_info("sigint_handler: shutting down");
171     shutdown_triggered = true;
172     hci_power_control(HCI_POWER_OFF);
173 }
174 
175 static int led_state = 0;
176 void hal_led_toggle(void){
177     led_state = 1 - led_state;
178     printf("LED State %u\n", led_state);
179 }
180 static void use_fast_uart(void){
181     printf("Using 921600 baud.\n");
182     config.baudrate_main = 921600;
183 }
184 
185 static void local_version_information_handler(uint8_t * packet){
186     printf("Local version information:\n");
187     uint16_t hci_version    = packet[6];
188     uint16_t hci_revision   = little_endian_read_16(packet, 7);
189     uint16_t lmp_version    = packet[9];
190     uint16_t manufacturer   = little_endian_read_16(packet, 10);
191     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
192     printf("- HCI Version    0x%04x\n", hci_version);
193     printf("- HCI Revision   0x%04x\n", hci_revision);
194     printf("- LMP Version    0x%04x\n", lmp_version);
195     printf("- LMP Subversion 0x%04x\n", lmp_subversion);
196     printf("- Manufacturer 0x%04x\n", manufacturer);
197     switch (manufacturer){
198         case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO:
199             printf("Cambridge Silicon Radio - CSR chipset, Build ID: %u.\n", hci_revision);
200             use_fast_uart();
201             hci_set_chipset(btstack_chipset_csr_instance());
202             break;
203         case BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC:
204             printf("Texas Instruments - CC256x compatible chipset.\n");
205             if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){
206                 printf("Error: LMP Subversion does not match initscript! ");
207                 printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer");
208                 printf("Please update Makefile to include the appropriate bluetooth_init_cc256???.c file\n");
209                 exit(10);
210             }
211             use_fast_uart();
212             hci_set_chipset(btstack_chipset_cc256x_instance());
213 #ifdef ENABLE_EHCILL
214             printf("eHCILL enabled.\n");
215 #else
216             printf("eHCILL disable.\n");
217 #endif
218 
219             break;
220         case BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION:
221             printf("Broadcom/Cypress - using BCM driver.\n");
222             hci_set_chipset(btstack_chipset_bcm_instance());
223             use_fast_uart();
224             is_bcm = 1;
225             break;
226         case BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS:
227             printf("ST Microelectronics - using STLC2500d driver.\n");
228             use_fast_uart();
229             hci_set_chipset(btstack_chipset_stlc2500d_instance());
230             break;
231         case BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA:
232             printf("EM Microelectronics - using EM9301 driver.\n");
233             hci_set_chipset(btstack_chipset_em9301_instance());
234             use_fast_uart();
235             break;
236         case BLUETOOTH_COMPANY_ID_NORDIC_SEMICONDUCTOR_ASA:
237             printf("Nordic Semiconductor nRF5 chipset.\n");
238             break;
239         case BLUETOOTH_COMPANY_ID_TOSHIBA_CORP:
240             printf("Toshiba - using TC3566x driver.\n");
241             hci_set_chipset(btstack_chipset_tc3566x_instance());
242             use_fast_uart();
243             break;
244         case BLUETOOTH_COMPANY_ID_PACKETCRAFT_INC:
245             printf("PacketCraft HCI Controller\n");
246             break;
247         default:
248             printf("Unknown manufacturer / manufacturer not supported yet.\n");
249             break;
250     }
251 }
252 
253 static char short_options[] = "hu:l:r";
254 
255 static struct option long_options[] = {
256         {"help",        no_argument,        NULL,   'h'},
257         {"logfile",    required_argument,  NULL,   'l'},
258         {"reset-tlv",    no_argument,       NULL,   'r'},
259         {"tty",    required_argument,  NULL,   'u'},
260         {0, 0, 0, 0}
261 };
262 
263 static char *help_options[] = {
264         "print (this) help.",
265         "set file to store debug output and HCI trace.",
266         "reset bonding information stored in TLV.",
267         "set path to Bluetooth Controller.",
268 };
269 
270 static char *option_arg_name[] = {
271         "",
272         "LOGFILE",
273         "",
274         "TTY",
275 };
276 
277 static void usage(const char *name){
278     unsigned int i;
279     printf( "usage:\n\t%s [options]\n", name );
280     printf("valid options:\n");
281     for( i=0; long_options[i].name != 0; i++) {
282         printf("--%-10s| -%c  %-10s\t\t%s\n", long_options[i].name, long_options[i].val, option_arg_name[i], help_options[i] );
283     }
284 }
285 
286 int main(int argc, const char * argv[]){
287 
288     const char * log_file_path = NULL;
289 
290     // set default device path
291     config.device_name = "/dev/tty.usbmodemEF437DF524C51";
292 
293     // parse command line parameters
294     while(true){
295         int c = getopt_long( argc, (char* const *)argv, short_options, long_options, NULL );
296         if (c < 0) {
297             break;
298         }
299         if (c == '?'){
300             break;
301         }
302         switch (c) {
303             case 'u':
304                 config.device_name = optarg;
305                 break;
306             case 'l':
307                 log_file_path = optarg;
308                 break;
309             case 'r':
310                 tlv_reset = true;
311                 break;
312             case 'h':
313             default:
314                 usage(argv[0]);
315                 return EXIT_FAILURE;
316         }
317     }
318     /// GET STARTED with BTstack ///
319 	btstack_memory_init();
320     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
321 
322     // log into file using HCI_DUMP_PACKETLOGGER format
323     if (log_file_path == NULL){
324         log_file_path = "/tmp/hci_dump.pklg";
325     }
326     hci_dump_posix_fs_open(log_file_path, HCI_DUMP_PACKETLOGGER);
327     const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance();
328     hci_dump_init(hci_dump_impl);
329     printf("Packet Log: %s\n", log_file_path);
330 
331     printf("H4 device: %s\n", config.device_name);
332 
333     // init HCI
334     const btstack_uart_t * uart_driver = btstack_uart_posix_instance();
335 	const hci_transport_t * transport = hci_transport_h4_instance_for_uart(uart_driver);
336 	hci_init(transport, (void*) &config);
337 
338 #ifdef HAVE_PORTAUDIO
339     btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance());
340     btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance());
341 #endif
342 
343     // set BD_ADDR for CSR without Flash/unique address
344     // bd_addr_t own_address = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
345     // btstack_chipset_csr_set_bd_addr(own_address);
346 
347     // inform about BTstack state
348     hci_event_callback_registration.callback = &packet_handler;
349     hci_add_event_handler(&hci_event_callback_registration);
350 
351     // register callback for CTRL-c
352     btstack_signal_register_callback(SIGINT, &trigger_shutdown);
353 
354     // setup app
355     btstack_main(argc, argv);
356 
357     // go
358     btstack_run_loop_execute();
359 
360     return 0;
361 }
362