xref: /btstack/port/windows-h4/main.c (revision b29e92f97ffd81bc8a1057634c8d1552fc963a6a)
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 #include <stdint.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #include "btstack_config.h"
46 
47 #include "ble/le_device_db_tlv.h"
48 #include "bluetooth_company_id.h"
49 #include "btstack_chipset_bcm.h"
50 #include "btstack_chipset_cc256x.h"
51 #include "btstack_chipset_csr.h"
52 #include "btstack_chipset_em9301.h"
53 #include "btstack_chipset_stlc2500d.h"
54 #include "btstack_chipset_tc3566x.h"
55 #include "btstack_debug.h"
56 #include "btstack_event.h"
57 #include "btstack_memory.h"
58 #include "btstack_run_loop.h"
59 #include "btstack_run_loop_windows.h"
60 #include "btstack_stdin.h"
61 #include "btstack_stdin_windows.h"
62 #include "btstack_tlv_windows.h"
63 #include "classic/btstack_link_key_db_tlv.h"
64 #include "hal_led.h"
65 #include "hci.h"
66 #include "hci_dump.h"
67 #include "hci_dump_windows_fs.h"
68 #include "hci_transport.h"
69 #include "hci_transport_h4.h"
70 
71 int btstack_main(int argc, const char * argv[]);
72 static void local_version_information_handler(uint8_t * packet);
73 
74 static hci_transport_config_uart_t config = {
75         HCI_TRANSPORT_CONFIG_UART,
76         115200,
77         0,  // main baudrate
78         1,  // flow control
79         NULL,
80 };
81 
82 int is_bcm;
83 
84 static int led_state = 0;
85 
86 #define TLV_DB_PATH_PREFIX "btstack_"
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_windows_t   tlv_context;
91 static bd_addr_t             local_addr;
92 static bool shutdown_triggered;
93 
94 void hal_led_toggle(void){
95     led_state = 1 - led_state;
96     printf("LED State %u\n", led_state);
97 }
98 
99 static void trigger_shutdown(void){
100     printf("CTRL-C - SIGINT received, shutting down..\n");
101     log_info("sigint_handler: shutting down");
102     shutdown_triggered = true;
103     hci_power_control(HCI_POWER_OFF);
104 }
105 
106 static btstack_packet_callback_registration_t hci_event_callback_registration;
107 
108 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
109     if (packet_type != HCI_EVENT_PACKET) return;
110     switch (hci_event_packet_get_type(packet)){
111         case BTSTACK_EVENT_STATE:
112             switch (btstack_event_state_get_state(packet)){
113                 case HCI_STATE_WORKING:
114                     gap_local_bd_addr(local_addr);
115                     printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
116                     btstack_strcpy(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_PREFIX);
117                     btstack_strcat(tlv_db_path, sizeof(tlv_db_path), bd_addr_to_str_with_delimiter(local_addr, '-'));
118                     btstack_strcat(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_POSTFIX);
119                     tlv_impl = btstack_tlv_windows_init_instance(&tlv_context, tlv_db_path);
120                     btstack_tlv_set_instance(tlv_impl, &tlv_context);
121 #ifdef ENABLE_CLASSIC
122                     hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
123 #endif
124 #ifdef ENABLE_BLE
125                     le_device_db_tlv_configure(tlv_impl, &tlv_context);
126 #endif
127                     break;
128                 case HCI_STATE_OFF:
129                     btstack_tlv_windows_deinit(&tlv_context);
130                     if (!shutdown_triggered) break;
131                     // reset stdin
132                     btstack_stdin_reset();
133                     log_info("Good bye, see you.\n");
134                     exit(0);
135                     break;
136                 default:
137                     break;
138             }
139             break;
140         case HCI_EVENT_COMMAND_COMPLETE:
141             if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_NAME){
142                 if (hci_event_command_complete_get_return_parameters(packet)[0]) break;
143                 // terminate, name 248 chars
144                 packet[6+248] = 0;
145                 printf("Local name: %s\n", &packet[6]);
146                 if (is_bcm){
147                     btstack_chipset_bcm_set_device_name((const char *)&packet[6]);
148                 }
149             }
150             if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){
151                 local_version_information_handler(packet);
152             }
153             break;
154         default:
155             break;
156     }
157 }
158 static void use_fast_uart(void){
159     printf("Using 921600 baud.\n");
160     config.baudrate_main = 921600;
161 }
162 
163 static void local_version_information_handler(uint8_t * packet){
164     printf("Local version information:\n");
165     uint16_t hci_version    = packet[6];
166     uint16_t hci_revision   = little_endian_read_16(packet, 7);
167     uint16_t lmp_version    = packet[9];
168     uint16_t manufacturer   = little_endian_read_16(packet, 10);
169     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
170     printf("- HCI Version  0x%04x\n", hci_version);
171     printf("- HCI Revision 0x%04x\n", hci_revision);
172     printf("- LMP Version  0x%04x\n", lmp_version);
173     printf("- LMP Revision 0x%04x\n", lmp_subversion);
174     printf("- Manufacturer 0x%04x\n", manufacturer);
175     switch (manufacturer){
176         case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO:
177             printf("Cambridge Silicon Radio - CSR chipset, Build ID: %u.\n", hci_revision);
178             use_fast_uart();
179             hci_set_chipset(btstack_chipset_csr_instance());
180             break;
181         case BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC:
182             printf("Texas Instruments - CC256x compatible chipset.\n");
183             if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){
184                 printf("Error: LMP Subversion does not match initscript!");
185                 printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer");
186                 printf("Please update Makefile to include the appropriate bluetooth_init_cc256???.c file\n");
187                 exit(10);
188             }
189             use_fast_uart();
190             hci_set_chipset(btstack_chipset_cc256x_instance());
191 #ifdef ENABLE_EHCILL
192             printf("eHCILL enabled.\n");
193 #else
194             printf("eHCILL disable.\n");
195 #endif
196             break;
197         case BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION:
198             printf("Broadcom - using BCM driver.\n");
199             hci_set_chipset(btstack_chipset_bcm_instance());
200             use_fast_uart();
201             is_bcm = 1;
202             break;
203         case BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS:
204             printf("ST Microelectronics - using STLC2500d driver.\n");
205             use_fast_uart();
206             hci_set_chipset(btstack_chipset_stlc2500d_instance());
207             break;
208         case BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA:
209             printf("EM Microelectronics - using EM9301 driver.\n");
210             hci_set_chipset(btstack_chipset_em9301_instance());
211             break;
212         case BLUETOOTH_COMPANY_ID_NORDIC_SEMICONDUCTOR_ASA:
213             printf("Nordic Semiconductor nRF5 chipset.\n");
214             break;
215         case BLUETOOTH_COMPANY_ID_TOSHIBA_CORP:
216             printf("Toshiba - using TC3566x driver.\n");
217             hci_set_chipset(btstack_chipset_tc3566x_instance());
218             use_fast_uart();
219             break;
220         default:
221             printf("Unknown manufacturer / manufacturer not supported yet.\n");
222             break;
223     }
224 }
225 
226 int main(int argc, const char * argv[]){
227     printf("BTstack on windows booting up\n");
228 
229     /// GET STARTED with BTstack ///
230     btstack_memory_init();
231     btstack_run_loop_init(btstack_run_loop_windows_get_instance());
232 
233     // log into file using HCI_DUMP_PACKETLOGGER format
234     const char * pklg_path = "hci_dump.pklg";
235     hci_dump_windows_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER);
236     const hci_dump_t * hci_dump_impl = hci_dump_windows_fs_get_instance();
237     hci_dump_init(hci_dump_impl);
238     printf("Packet Log: %s\n", pklg_path);
239 
240     // pick serial port
241     config.device_name = "\\\\.\\COM7";
242 
243     // accept path from command line
244     if (argc >= 3 && strcmp(argv[1], "-u") == 0){
245         config.device_name = argv[2];
246         argc -= 2;
247         memmove((void *) &argv[1], &argv[3], (argc-1) * sizeof(char *));
248     }
249     printf("H4 device: %s\n", config.device_name);
250 
251     // init HCI
252     const btstack_uart_block_t * uart_driver = btstack_uart_block_windows_instance();
253     const hci_transport_t * transport = hci_transport_h4_instance(uart_driver);
254     hci_init(transport, (void*) &config);
255 
256     // inform about BTstack state
257     hci_event_callback_registration.callback = &packet_handler;
258     hci_add_event_handler(&hci_event_callback_registration);
259 
260     // setup stdin to handle CTRL-c
261     btstack_stdin_windows_init();
262     btstack_stdin_window_register_ctrl_c_callback(&trigger_shutdown);
263 
264     // setup app
265     btstack_main(argc, argv);
266 
267     // go
268     btstack_run_loop_execute();
269 
270     return 0;
271 }
272