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