xref: /btstack/port/windows-h4/main.c (revision 634a9874c04cc1618af15af1817d00d8dea76e3f)
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 static void local_version_information_handler(uint8_t * packet);
30 
31 static hci_transport_config_uart_t config = {
32     HCI_TRANSPORT_CONFIG_UART,
33     115200,
34     0,  // main baudrate
35     1,  // flow control
36     NULL,
37 };
38 
39 int is_bcm;
40 
41 static int led_state = 0;
42 
43 void hal_led_toggle(void){
44     led_state = 1 - led_state;
45     printf("LED State %u\n", led_state);
46 }
47 
48 static void sigint_handler(int param){
49 
50     // reset even if not setup before
51     btstack_stdin_reset();
52 
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 (is_bcm){
75                     btstack_chipset_bcm_set_device_name((const char *)&packet[6]);
76                 }
77             }
78             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){
79                 local_version_information_handler(packet);
80             }
81             break;
82 
83         default:
84             break;
85     }
86 }
87 
88 static void use_fast_uart(void){
89     printf("Using 921600 baud.\n");
90     config.baudrate_main = 921600;
91 }
92 
93 static void local_version_information_handler(uint8_t * packet){
94     printf("Local version information:\n");
95     uint16_t hci_version    = little_endian_read_16(packet, 4);
96     uint16_t hci_revision   = little_endian_read_16(packet, 6);
97     uint16_t lmp_version    = little_endian_read_16(packet, 8);
98     uint16_t manufacturer   = little_endian_read_16(packet, 10);
99     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
100     printf("- HCI Version  0x%04x\n", hci_version);
101     printf("- HCI Revision 0x%04x\n", hci_revision);
102     printf("- LMP Version  0x%04x\n", lmp_version);
103     printf("- LMP Revision 0x%04x\n", lmp_subversion);
104     printf("- Manufacturer 0x%04x\n", manufacturer);
105     switch (manufacturer){
106         case COMPANY_ID_CAMBRIDGE_SILICON_RADIO:
107             printf("Cambridge Silicon Radio - CSR chipset.\n");
108             use_fast_uart();
109             hci_set_chipset(btstack_chipset_csr_instance());
110             break;
111         case COMPANY_ID_TEXAS_INSTRUMENTS_INC:
112             printf("Texas Instruments - CC256x compatible chipset.\n");
113             if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){
114                 printf("Error: LMP Subversion does not match initscript!");
115                 printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer");
116                 printf("Please update Makefile to include the appropriate bluetooth_init_cc256???.c file\n");
117                 exit(10);
118             }
119             use_fast_uart();
120             hci_set_chipset(btstack_chipset_cc256x_instance());
121 #ifdef ENABLE_EHCILL
122             printf("eHCILL enabled.\n");
123 #else
124             printf("eHCILL disable.\n");
125 #endif
126             break;
127         case COMPANY_ID_BROADCOM_CORPORATION:
128             printf("Broadcom - using BCM driver.\n");
129             hci_set_chipset(btstack_chipset_bcm_instance());
130             use_fast_uart();
131             is_bcm = 1;
132             break;
133         case COMPANY_ID_ST_MICROELECTRONICS:
134             printf("ST Microelectronics - using STLC2500d driver.\n");
135             use_fast_uart();
136             hci_set_chipset(btstack_chipset_stlc2500d_instance());
137             break;
138         case COMPANY_ID_EM_MICROELECTRONICS_MARIN:
139             printf("EM Microelectronics - using EM9301 driver.\n");
140             hci_set_chipset(btstack_chipset_em9301_instance());
141             break;
142         case COMPANY_ID_NORDIC_SEMICONDUCTOR_ASA:
143             printf("Nordic Semiconductor nRF5 chipset.\n");
144             break;
145         default:
146             printf("Unknown manufacturer / manufacturer not supported yet.\n");
147             break;
148     }
149 }
150 
151 int main(int argc, const char * argv[]){
152 	printf("BTstack on windows booting up\n");
153 
154 	/// GET STARTED with BTstack ///
155 	btstack_memory_init();
156     btstack_run_loop_init(btstack_run_loop_windows_get_instance());
157 
158     hci_dump_open("hci_dump.pklg", HCI_DUMP_PACKETLOGGER);
159 
160     // pick serial port
161     config.device_name = "\\\\.\\COM7";
162 
163     // init HCI
164     const btstack_uart_block_t * uart_driver = btstack_uart_block_windows_instance();
165 	const hci_transport_t * transport = hci_transport_h4_instance(uart_driver);
166     const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance();
167 	hci_init(transport, (void*) &config);
168     hci_set_link_key_db(link_key_db);
169 
170     // inform about BTstack state
171     hci_event_callback_registration.callback = &packet_handler;
172     hci_add_event_handler(&hci_event_callback_registration);
173 
174     // handle CTRL-c
175     signal(SIGINT, sigint_handler);
176 
177     // setup app
178     btstack_main(argc, argv);
179 
180     // go
181     btstack_run_loop_execute();
182 
183 	return 0;
184 }
185