xref: /btstack/port/qt-h4/main.cpp (revision f25e60dece291970e0c7612fc7fda3d74ae3ceba)
1 /*
2  * Copyright (C) 2019 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 <QCoreApplication>
47 
48 #include <stdint.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <signal.h>
53 
54 #include "btstack_config.h"
55 
56 #include "bluetooth_company_id.h"
57 #include "btstack_debug.h"
58 #include "btstack_event.h"
59 #include "ble/le_device_db_tlv.h"
60 #include "classic/btstack_link_key_db_tlv.h"
61 #include "btstack_memory.h"
62 #include "btstack_run_loop.h"
63 #include "btstack_run_loop_qt.h"
64 #incldue "btstack_uart.h"
65 #include "hal_led.h"
66 #include "hci.h"
67 #include "hci_dump.h"
68 #include "hci_dump_posix_fs.h"
69 #include "hci_transport.h"
70 #include "hci_transport_h4.h"
71 #include "btstack_stdin.h"
72 #include "btstack_audio.h"
73 #include "btstack_tlv_posix.h"
74 #include "btstack_uart_block.h"
75 
76 // chipsets
77 #include "btstack_chipset_bcm.h"
78 #include "btstack_chipset_csr.h"
79 #include "btstack_chipset_cc256x.h"
80 
81 #ifdef Q_OS_WIN
82 #include "btstack_stdin_windows.h"
83 #else
84 #include <signal.h>
85 #include "btstack_signal.h"
86 #endif
87 
88 #ifdef Q_OS_WIN
89 #define TLV_DB_PATH_PREFIX "btstack"
90 #else
91 #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
92 #endif
93 
94 #define TLV_DB_PATH_POSTFIX ".tlv"
95 static char tlv_db_path[100];
96 static const btstack_tlv_t * tlv_impl;
97 static btstack_tlv_posix_t   tlv_context;
98 static bd_addr_t             local_addr;
99 static int is_bcm;
100 
101 // shutdown
102 static bool shutdown_triggered;
103 
104 extern "C" int btstack_main(int argc, const char * argv[]);
105 
106 static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc };
107 
108 static bd_addr_t static_address;
109 static int using_static_address;
110 
111 static btstack_packet_callback_registration_t hci_event_callback_registration;
112 
113 // H4
114 static hci_transport_config_uart_t config = {
115     HCI_TRANSPORT_CONFIG_UART,
116     115200,
117     0,  // main baudrate
118     1,  // flow control
119     NULL,
120 };
121 
122 static void use_fast_uart(void){
123     printf("Using 921600 baud.\n");
124     config.baudrate_main = 921600;
125 }
126 
127 static void local_version_information_handler(uint8_t * packet){
128     printf("Local version information:\n");
129     uint16_t hci_version    = packet[6];
130     uint16_t hci_revision   = little_endian_read_16(packet, 7);
131     uint16_t lmp_version    = packet[9];
132     uint16_t manufacturer   = little_endian_read_16(packet, 10);
133     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
134     printf("- HCI Version    0x%04x\n", hci_version);
135     printf("- HCI Revision   0x%04x\n", hci_revision);
136     printf("- LMP Version    0x%04x\n", lmp_version);
137     printf("- LMP Subversion 0x%04x\n", lmp_subversion);
138     printf("- Manufacturer 0x%04x\n", manufacturer);
139     switch (manufacturer){
140         case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO:
141             printf("Cambridge Silicon Radio - CSR chipset, Build ID: %u.\n", hci_revision);
142             use_fast_uart();
143             hci_set_chipset(btstack_chipset_csr_instance());
144             break;
145         case BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC:
146             printf("Texas Instruments - CC256x compatible chipset.\n");
147             if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){
148                 printf("Error: LMP Subversion does not match initscript! ");
149                 printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer");
150                 printf("Please update CMakeLists.txt to include the appropriate bluetooth_init_cc256???.c file\n");
151                 exit(10);
152             }
153             use_fast_uart();
154             hci_set_chipset(btstack_chipset_cc256x_instance());
155 #ifdef ENABLE_EHCILL
156             printf("eHCILL enabled.\n");
157 #else
158             printf("eHCILL disable.\n");
159 #endif
160 
161             break;
162         case BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION:
163             printf("Broadcom/Cypress - using BCM driver.\n");
164             hci_set_chipset(btstack_chipset_bcm_instance());
165             use_fast_uart();
166             is_bcm = 1;
167             break;
168         default:
169             printf("Unknown manufacturer / manufacturer not supported yet.\n");
170             break;
171     }
172 }
173 
174 
175 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
176     UNUSED(channel);
177     UNUSED(size);
178     if (packet_type != HCI_EVENT_PACKET) return;
179     switch (hci_event_packet_get_type(packet)){
180         case BTSTACK_EVENT_STATE:
181             switch (btstack_event_state_get_state(packet)){
182                 case HCI_STATE_WORKING:
183                     gap_local_bd_addr(local_addr);
184                     if (using_static_address) {
185                         memcpy(local_addr, static_address, 6);
186                     }
187                     printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
188                     strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
189 #ifndef Q_OS_WIN
190                     // bd_addr_to_str use ":" which is not allowed in windows file names
191                     strcat(tlv_db_path, bd_addr_to_str(local_addr));
192 #endif
193                     strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
194                     tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
195                     btstack_tlv_set_instance(tlv_impl, &tlv_context);
196 #ifdef ENABLE_CLASSIC
197                     hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
198 #endif
199 #ifdef ENABLE_BLE
200                     le_device_db_tlv_configure(tlv_impl, &tlv_context);
201 #endif
202                     break;
203                 case HCI_STATE_OFF:
204                     btstack_tlv_posix_deinit(&tlv_context);
205                     if (!shutdown_triggered) break;
206                     // reset stdin
207                     btstack_stdin_reset();
208                     log_info("Good bye, see you.\n");
209                     exit(0);
210                     break;
211                 default:
212                     break;
213             }
214             break;
215         case HCI_EVENT_COMMAND_COMPLETE:
216             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
217                 if (hci_event_command_complete_get_return_parameters(packet)[0]) break;
218                 // terminate, name 248 chars
219                 packet[6+248] = 0;
220                 printf("Local name: %s\n", &packet[6]);
221                 if (is_bcm){
222                     btstack_chipset_bcm_set_device_name((const char *)&packet[6]);
223                 }
224             }
225             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){
226                 local_version_information_handler(packet);
227             }
228             break;
229         default:
230             break;
231     }
232 }
233 
234 static void trigger_shutdown(void){
235     printf("CTRL-C - SIGINT received, shutting down..\n");
236     log_info("sigint_handler: shutting down");
237     shutdown_triggered = true;
238     hci_power_control(HCI_POWER_OFF);
239 }
240 
241 static int led_state = 0;
242 void hal_led_toggle(void){
243     led_state = 1 - led_state;
244     printf("LED State %u\n", led_state);
245 }
246 
247 #define USB_MAX_PATH_LEN 7
248 int btstack_main(int argc, const char * argv[]);
249 
250 int main(int argc, char * argv[]){
251 
252     QCoreApplication a(argc, argv);
253 
254     /// GET STARTED with BTstack ///
255     btstack_memory_init();
256     btstack_run_loop_init(btstack_run_loop_qt_get_instance());
257 
258     // log into file using HCI_DUMP_PACKETLOGGER format
259     char pklg_path[100];
260 #ifdef Q_OS_WIN
261     strcpy(pklg_path, "hci_dump.pklg");
262 #else
263     strcpy(pklg_path, "/tmp/hci_dump.pklg");
264 #endif
265     hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER);
266     const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance();
267     hci_dump_init(hci_dump_impl);
268     printf("Packet Log: %s\n", pklg_path);
269 
270     // init HCI
271 #ifdef Q_OS_WIN
272     const btstack_uart_block_t * uart_driver = btstack_uart_block_windows_instance();
273     config.device_name = "\\\\.\\COM7";
274 #else
275     const btstack_uart_t * uart_driver = btstack_uart_posix_instance();
276     config.device_name = "/dev/tty.usbserial-A900K2WS"; // DFROBOT
277 #endif
278     const hci_transport_t * transport = hci_transport_h4_instance_for_uart(uart_driver);
279     hci_init(transport, (void*) &config);
280 
281 #ifdef HAVE_PORTAUDIO
282     btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance());
283     btstack_audio_source_set_instance(btstack_audio_portaudio_source_get_instance());
284 #endif
285 
286     // inform about BTstack state
287     hci_event_callback_registration.callback = &packet_handler;
288     hci_add_event_handler(&hci_event_callback_registration);
289 
290     // register callback for CTRL-c
291 #ifdef Q_OS_WIN
292     btstack_stdin_windows_init();
293     btstack_stdin_window_register_ctrl_c_callback(&trigger_shutdown);
294 #else
295     btstack_signal_register_callback(SIGINT, &trigger_shutdown);
296 #endif
297 
298     // setup app
299     btstack_main(argc, (const char **) argv);
300 
301     // enter Qt run loop
302     return a.exec();
303 }
304