xref: /btstack/port/windows-h4/main.c (revision c7e2dea5df4b1c023086abba9220641cae703cd2)
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <signal.h>
6 
7 #include "btstack_config.h"
8 
9 #include "btstack_debug.h"
10 #include "btstack_event.h"
11 #include "btstack_memory.h"
12 #include "btstack_run_loop.h"
13 #include "btstack_run_loop_windows.h"
14 #include "hci.h"
15 #include "hci_dump.h"
16 #include "hal_led.h"
17 #include "btstack_link_key_db_fs.h"
18 
19 // #include "stdin_support.h"
20 
21 #include "btstack_chipset_bcm.h"
22 #include "btstack_chipset_csr.h"
23 #include "btstack_chipset_cc256x.h"
24 #include "btstack_chipset_em9301.h"
25 #include "btstack_chipset_stlc2500d.h"
26 #include "btstack_chipset_tc3566x.h"
27 
28 int btstack_main(int argc, const char * argv[]);
29 
30 static hci_transport_config_uart_t config = {
31     HCI_TRANSPORT_CONFIG_UART,
32     115200,
33     0,  // main baudrate
34     1,  // flow control
35     NULL,
36 };
37 
38 int is_bcm;
39 
40 static int led_state = 0;
41 
42 void hal_led_toggle(void){
43     led_state = 1 - led_state;
44     printf("LED State %u\n", led_state);
45 }
46 
47 static void sigint_handler(int param){
48 
49 #ifndef _WIN32
50     // reset anyway
51     btstack_stdin_reset();
52 #endif
53     log_info(" <= SIGINT received, shutting down..\n");
54     // hci_power_control(HCI_POWER_OFF);
55     // hci_close();
56     log_info("Good bye, see you.\n");
57     exit(0);
58 }
59 
60 static btstack_packet_callback_registration_t hci_event_callback_registration;
61 
62 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
63     if (packet_type != HCI_EVENT_PACKET) return;
64     switch (hci_event_packet_get_type(packet)){
65         case BTSTACK_EVENT_STATE:
66             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
67             printf("BTstack up and running.\n");
68             break;
69         case HCI_EVENT_COMMAND_COMPLETE:
70             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
71                 // terminate, name 248 chars
72                 packet[6+248] = 0;
73                 printf("Local name: %s\n", &packet[6]);
74 #if 0
75                 if (is_bcm){
76                     btstack_chipset_bcm_set_device_name((const char *)&packet[6]);
77                 }
78 #endif
79             }
80             break;
81         default:
82             break;
83     }
84 }
85 
86 static void use_fast_uart(void){
87     printf("Using 921600 baud.\n");
88     config.baudrate_main = 921600;
89 }
90 
91 static void local_version_information_callback(uint8_t * packet){
92     printf("Local version information:\n");
93     uint16_t hci_version    = little_endian_read_16(packet, 4);
94     uint16_t hci_revision   = little_endian_read_16(packet, 6);
95     uint16_t lmp_version    = little_endian_read_16(packet, 8);
96     uint16_t manufacturer   = little_endian_read_16(packet, 10);
97     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
98     printf("- HCI Version  0x%04x\n", hci_version);
99     printf("- HCI Revision 0x%04x\n", hci_revision);
100     printf("- LMP Version  0x%04x\n", lmp_version);
101     printf("- LMP Revision 0x%04x\n", lmp_subversion);
102     printf("- Manufacturer 0x%04x\n", manufacturer);
103     switch (manufacturer){
104         case COMPANY_ID_CAMBRIDGE_SILICON_RADIO:
105             printf("Cambridge Silicon Radio - CSR chipset.\n");
106             use_fast_uart();
107             hci_set_chipset(btstack_chipset_csr_instance());
108             break;
109         case COMPANY_ID_TEXAS_INSTRUMENTS_INC:
110             printf("Texas Instruments - CC256x compatible chipset.\n");
111             if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){
112                 printf("Error: LMP Subversion does not match initscript!");
113                 printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer");
114                 printf("Please update Makefile to include the appropriate bluetooth_init_cc256???.c file\n");
115                 exit(10);
116             }
117             use_fast_uart();
118             hci_set_chipset(btstack_chipset_cc256x_instance());
119 #ifdef ENABLE_EHCILL
120             printf("eHCILL enabled.\n");
121 #else
122             printf("eHCILL disable.\n");
123 #endif
124             break;
125         case COMPANY_ID_BROADCOM_CORPORATION:
126             printf("Broadcom - not supported on Windows yet.\n");
127 #if 0
128             printf("Broadcom - using BCM driver.\n");
129             hci_set_chipset(btstack_chipset_bcm_instance());
130             use_fast_uart();
131             is_bcm = 1;
132 #else
133 #endif
134             break;
135         case COMPANY_ID_ST_MICROELECTRONICS:
136             printf("ST Microelectronics - using STLC2500d driver.\n");
137             use_fast_uart();
138             hci_set_chipset(btstack_chipset_stlc2500d_instance());
139             break;
140         case COMPANY_ID_EM_MICROELECTRONICS_MARIN:
141             printf("EM Microelectronics - using EM9301 driver.\n");
142             hci_set_chipset(btstack_chipset_em9301_instance());
143             break;
144         case COMPANY_ID_NORDIC_SEMICONDUCTOR_ASA:
145             printf("Nordic Semiconductor nRF5 chipset.\n");
146             break;
147         default:
148             printf("Unknown manufacturer / manufacturer not supported yet.\n");
149             break;
150     }
151 }
152 
153 int main(int argc, const char * argv[]){
154 	printf("BTstack on windows booting up\n");
155 
156 	/// GET STARTED with BTstack ///
157 	btstack_memory_init();
158     btstack_run_loop_init(btstack_run_loop_windows_get_instance());
159 
160     hci_dump_open(NULL, HCI_DUMP_STDOUT);
161 
162     // pick serial port
163     config.device_name = "\\\\.\\COM7";
164 
165     // init HCI
166     const btstack_uart_block_t * uart_driver = btstack_uart_block_windows_instance();
167 	const hci_transport_t * transport = hci_transport_h4_instance(uart_driver);
168     const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance();
169 	hci_init(transport, (void*) &config);
170     hci_set_link_key_db(link_key_db);
171 
172     // inform about BTstack state
173     hci_event_callback_registration.callback = &packet_handler;
174     hci_add_event_handler(&hci_event_callback_registration);
175 
176     // setup dynamic chipset driver setup
177     hci_set_local_version_information_callback(&local_version_information_callback);
178 
179     // handle CTRL-c
180     signal(SIGINT, sigint_handler);
181 
182     // setup app
183     btstack_main(argc, argv);
184 
185     // go
186     btstack_run_loop_execute();
187 
188 	return 0;
189 }
190