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