xref: /btstack/port/windows-h4/main.c (revision c5541f687d332c2598df967dc213b833a570b7c5)
1ab2c6ae4SMatthias Ringwald /*
2ab2c6ae4SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3ab2c6ae4SMatthias Ringwald  *
4ab2c6ae4SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5ab2c6ae4SMatthias Ringwald  * modification, are permitted provided that the following conditions
6ab2c6ae4SMatthias Ringwald  * are met:
7ab2c6ae4SMatthias Ringwald  *
8ab2c6ae4SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9ab2c6ae4SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10ab2c6ae4SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11ab2c6ae4SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12ab2c6ae4SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13ab2c6ae4SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14ab2c6ae4SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15ab2c6ae4SMatthias Ringwald  *    from this software without specific prior written permission.
16ab2c6ae4SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17ab2c6ae4SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18ab2c6ae4SMatthias Ringwald  *    monetary gain.
19ab2c6ae4SMatthias Ringwald  *
20ab2c6ae4SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21ab2c6ae4SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22ab2c6ae4SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23ab2c6ae4SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24ab2c6ae4SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25ab2c6ae4SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26ab2c6ae4SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27ab2c6ae4SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28ab2c6ae4SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29ab2c6ae4SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30ab2c6ae4SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31ab2c6ae4SMatthias Ringwald  * SUCH DAMAGE.
32ab2c6ae4SMatthias Ringwald  *
33ab2c6ae4SMatthias Ringwald  * Please inquire about commercial licensing options at
34ab2c6ae4SMatthias Ringwald  * [email protected]
35ab2c6ae4SMatthias Ringwald  *
36ab2c6ae4SMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "main.c"
39ab2c6ae4SMatthias Ringwald 
404e630824SMatthias Ringwald #include <stdint.h>
414e630824SMatthias Ringwald #include <stdio.h>
424e630824SMatthias Ringwald #include <stdlib.h>
434e630824SMatthias Ringwald #include <string.h>
444e630824SMatthias Ringwald #include <signal.h>
454e630824SMatthias Ringwald 
464e630824SMatthias Ringwald #include "btstack_config.h"
47fbae2915SMatthias Ringwald #include "bluetooth_company_id.h"
484e630824SMatthias Ringwald 
494e630824SMatthias Ringwald #include "btstack_debug.h"
504e630824SMatthias Ringwald #include "btstack_event.h"
514e630824SMatthias Ringwald #include "btstack_memory.h"
524e630824SMatthias Ringwald #include "btstack_run_loop.h"
534e630824SMatthias Ringwald #include "btstack_run_loop_windows.h"
544e630824SMatthias Ringwald #include "hci.h"
554e630824SMatthias Ringwald #include "hci_dump.h"
564e630824SMatthias Ringwald #include "hal_led.h"
57422979b1SMatthias Ringwald #include "btstack_link_key_db_fs.h"
58422979b1SMatthias Ringwald 
597ea7688aSMatthias Ringwald #include "btstack_stdin.h"
604e630824SMatthias Ringwald 
61422979b1SMatthias Ringwald #include "btstack_chipset_bcm.h"
62422979b1SMatthias Ringwald #include "btstack_chipset_csr.h"
63422979b1SMatthias Ringwald #include "btstack_chipset_cc256x.h"
64422979b1SMatthias Ringwald #include "btstack_chipset_em9301.h"
65422979b1SMatthias Ringwald #include "btstack_chipset_stlc2500d.h"
66422979b1SMatthias Ringwald #include "btstack_chipset_tc3566x.h"
67422979b1SMatthias Ringwald 
6827ff675bSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
69634a9874SMatthias Ringwald static void local_version_information_handler(uint8_t * packet);
7027ff675bSMatthias Ringwald 
71422979b1SMatthias Ringwald static hci_transport_config_uart_t config = {
72422979b1SMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
73422979b1SMatthias Ringwald     115200,
74422979b1SMatthias Ringwald     0,  // main baudrate
75422979b1SMatthias Ringwald     1,  // flow control
76422979b1SMatthias Ringwald     NULL,
77422979b1SMatthias Ringwald };
78422979b1SMatthias Ringwald 
79422979b1SMatthias Ringwald int is_bcm;
80422979b1SMatthias Ringwald 
814e630824SMatthias Ringwald static int led_state = 0;
82422979b1SMatthias Ringwald 
834e630824SMatthias Ringwald void hal_led_toggle(void){
844e630824SMatthias Ringwald     led_state = 1 - led_state;
854e630824SMatthias Ringwald     printf("LED State %u\n", led_state);
864e630824SMatthias Ringwald }
874e630824SMatthias Ringwald 
8827ff675bSMatthias Ringwald static void sigint_handler(int param){
89b96c8f44SMatthias Ringwald     UNUSED(param);
9027ff675bSMatthias Ringwald 
91b96c8f44SMatthias Ringwald     printf("CTRL-C = SIGINT received, shutting down..\n");
92b96c8f44SMatthias Ringwald     log_info("sigint_handler: shutting down");
93b96c8f44SMatthias Ringwald 
94b96c8f44SMatthias Ringwald     // reset anyway
9527ff675bSMatthias Ringwald     btstack_stdin_reset();
96341c09eeSMatthias Ringwald 
97b96c8f44SMatthias Ringwald     // power down
98b96c8f44SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
99b96c8f44SMatthias Ringwald     hci_close();
10027ff675bSMatthias Ringwald     log_info("Good bye, see you.\n");
10127ff675bSMatthias Ringwald     exit(0);
10227ff675bSMatthias Ringwald }
10327ff675bSMatthias Ringwald 
104422979b1SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
105422979b1SMatthias Ringwald 
106422979b1SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
107cf524f2bSMatthias Ringwald     bd_addr_t addr;
108422979b1SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
109422979b1SMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
110422979b1SMatthias Ringwald         case BTSTACK_EVENT_STATE:
111422979b1SMatthias Ringwald             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
112cf524f2bSMatthias Ringwald             gap_local_bd_addr(addr);
113cf524f2bSMatthias Ringwald             printf("BTstack up and running at %s\n",  bd_addr_to_str(addr));
114422979b1SMatthias Ringwald             break;
115422979b1SMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
116422979b1SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
117cf524f2bSMatthias Ringwald                 if (hci_event_command_complete_get_return_parameters(packet)[0]) break;
118422979b1SMatthias Ringwald                 // terminate, name 248 chars
119422979b1SMatthias Ringwald                 packet[6+248] = 0;
120422979b1SMatthias Ringwald                 printf("Local name: %s\n", &packet[6]);
121422979b1SMatthias Ringwald                 if (is_bcm){
122422979b1SMatthias Ringwald                     btstack_chipset_bcm_set_device_name((const char *)&packet[6]);
123422979b1SMatthias Ringwald                 }
124422979b1SMatthias Ringwald             }
125634a9874SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){
126634a9874SMatthias Ringwald                 local_version_information_handler(packet);
127634a9874SMatthias Ringwald             }
128422979b1SMatthias Ringwald             break;
129422979b1SMatthias Ringwald         default:
130422979b1SMatthias Ringwald             break;
131422979b1SMatthias Ringwald     }
132422979b1SMatthias Ringwald }
133422979b1SMatthias Ringwald static void use_fast_uart(void){
134422979b1SMatthias Ringwald     printf("Using 921600 baud.\n");
135422979b1SMatthias Ringwald     config.baudrate_main = 921600;
136422979b1SMatthias Ringwald }
137422979b1SMatthias Ringwald 
138634a9874SMatthias Ringwald static void local_version_information_handler(uint8_t * packet){
139422979b1SMatthias Ringwald     printf("Local version information:\n");
1405c2e0bddSMatthias Ringwald     uint16_t hci_version    = packet[6];
1415c2e0bddSMatthias Ringwald     uint16_t hci_revision   = little_endian_read_16(packet, 7);
1425c2e0bddSMatthias Ringwald     uint16_t lmp_version    = packet[9];
143422979b1SMatthias Ringwald     uint16_t manufacturer   = little_endian_read_16(packet, 10);
144422979b1SMatthias Ringwald     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
145422979b1SMatthias Ringwald     printf("- HCI Version  0x%04x\n", hci_version);
146422979b1SMatthias Ringwald     printf("- HCI Revision 0x%04x\n", hci_revision);
147422979b1SMatthias Ringwald     printf("- LMP Version  0x%04x\n", lmp_version);
148422979b1SMatthias Ringwald     printf("- LMP Revision 0x%04x\n", lmp_subversion);
149422979b1SMatthias Ringwald     printf("- Manufacturer 0x%04x\n", manufacturer);
150422979b1SMatthias Ringwald     switch (manufacturer){
15161f37892SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO:
152422979b1SMatthias Ringwald             printf("Cambridge Silicon Radio - CSR chipset.\n");
153422979b1SMatthias Ringwald             use_fast_uart();
154422979b1SMatthias Ringwald             hci_set_chipset(btstack_chipset_csr_instance());
155422979b1SMatthias Ringwald             break;
15661f37892SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_TEXAS_INSTRUMENTS_INC:
157422979b1SMatthias Ringwald             printf("Texas Instruments - CC256x compatible chipset.\n");
158c7e2dea5SMatthias Ringwald             if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){
159c7e2dea5SMatthias Ringwald                 printf("Error: LMP Subversion does not match initscript!");
160c7e2dea5SMatthias Ringwald                 printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer");
161c7e2dea5SMatthias Ringwald                 printf("Please update Makefile to include the appropriate bluetooth_init_cc256???.c file\n");
162c7e2dea5SMatthias Ringwald                 exit(10);
163c7e2dea5SMatthias Ringwald             }
164422979b1SMatthias Ringwald             use_fast_uart();
165422979b1SMatthias Ringwald             hci_set_chipset(btstack_chipset_cc256x_instance());
166422979b1SMatthias Ringwald #ifdef ENABLE_EHCILL
167422979b1SMatthias Ringwald             printf("eHCILL enabled.\n");
168422979b1SMatthias Ringwald #else
169422979b1SMatthias Ringwald             printf("eHCILL disable.\n");
170422979b1SMatthias Ringwald #endif
171422979b1SMatthias Ringwald             break;
17261f37892SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION:
173422979b1SMatthias Ringwald             printf("Broadcom - using BCM driver.\n");
174422979b1SMatthias Ringwald             hci_set_chipset(btstack_chipset_bcm_instance());
175422979b1SMatthias Ringwald             use_fast_uart();
176422979b1SMatthias Ringwald             is_bcm = 1;
177422979b1SMatthias Ringwald             break;
17861f37892SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS:
179422979b1SMatthias Ringwald             printf("ST Microelectronics - using STLC2500d driver.\n");
180422979b1SMatthias Ringwald             use_fast_uart();
181422979b1SMatthias Ringwald             hci_set_chipset(btstack_chipset_stlc2500d_instance());
182422979b1SMatthias Ringwald             break;
183fbae2915SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA:
184422979b1SMatthias Ringwald             printf("EM Microelectronics - using EM9301 driver.\n");
185422979b1SMatthias Ringwald             hci_set_chipset(btstack_chipset_em9301_instance());
186422979b1SMatthias Ringwald             break;
18761f37892SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_NORDIC_SEMICONDUCTOR_ASA:
188422979b1SMatthias Ringwald             printf("Nordic Semiconductor nRF5 chipset.\n");
189422979b1SMatthias Ringwald             break;
190*c5541f68SMatthias Ringwald         case BLUETOOTH_COMPANY_ID_TOSHIBA_CORP:
191*c5541f68SMatthias Ringwald             printf("Toshiba - using TC3566x driver.\n");
192*c5541f68SMatthias Ringwald             hci_set_chipset(btstack_chipset_tc3566x_instance());
193*c5541f68SMatthias Ringwald             use_fast_uart();
194*c5541f68SMatthias Ringwald             break;
195422979b1SMatthias Ringwald         default:
196422979b1SMatthias Ringwald             printf("Unknown manufacturer / manufacturer not supported yet.\n");
197422979b1SMatthias Ringwald             break;
198422979b1SMatthias Ringwald     }
199422979b1SMatthias Ringwald }
200422979b1SMatthias Ringwald 
20127ff675bSMatthias Ringwald int main(int argc, const char * argv[]){
2024e630824SMatthias Ringwald 	printf("BTstack on windows booting up\n");
2034e630824SMatthias Ringwald 
2044e630824SMatthias Ringwald 	/// GET STARTED with BTstack ///
2054e630824SMatthias Ringwald 	btstack_memory_init();
2064e630824SMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_windows_get_instance());
2074e630824SMatthias Ringwald 
2085096a6aeSMatthias Ringwald     hci_dump_open("hci_dump.pklg", HCI_DUMP_PACKETLOGGER);
209422979b1SMatthias Ringwald 
210422979b1SMatthias Ringwald     // pick serial port
211422979b1SMatthias Ringwald     config.device_name = "\\\\.\\COM7";
212422979b1SMatthias Ringwald 
213422979b1SMatthias Ringwald     // init HCI
214422979b1SMatthias Ringwald     const btstack_uart_block_t * uart_driver = btstack_uart_block_windows_instance();
215422979b1SMatthias Ringwald 	const hci_transport_t * transport = hci_transport_h4_instance(uart_driver);
216422979b1SMatthias Ringwald     const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance();
217422979b1SMatthias Ringwald 	hci_init(transport, (void*) &config);
218422979b1SMatthias Ringwald     hci_set_link_key_db(link_key_db);
219422979b1SMatthias Ringwald 
220422979b1SMatthias Ringwald     // inform about BTstack state
221422979b1SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
222422979b1SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
223422979b1SMatthias Ringwald 
22427ff675bSMatthias Ringwald     // handle CTRL-c
22527ff675bSMatthias Ringwald     signal(SIGINT, sigint_handler);
22627ff675bSMatthias Ringwald 
22727ff675bSMatthias Ringwald     // setup app
22827ff675bSMatthias Ringwald     btstack_main(argc, argv);
22927ff675bSMatthias Ringwald 
23027ff675bSMatthias Ringwald     // go
23127ff675bSMatthias Ringwald     btstack_run_loop_execute();
23227ff675bSMatthias Ringwald 
2334e630824SMatthias Ringwald 	return 0;
2344e630824SMatthias Ringwald }
235