xref: /btstack/port/windows-h4/main.c (revision 422979b135292697db314abd292512e4b75a86f8)
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             use_fast_uart();
112             hci_set_chipset(btstack_chipset_cc256x_instance());
113 #ifdef ENABLE_EHCILL
114             printf("eHCILL enabled.\n");
115 #else
116             printf("eHCILL disable.\n");
117 #endif
118             break;
119         case COMPANY_ID_BROADCOM_CORPORATION:
120             printf("Broadcom - not supported on Windows yet.\n");
121 #if 0
122             printf("Broadcom - using BCM driver.\n");
123             hci_set_chipset(btstack_chipset_bcm_instance());
124             use_fast_uart();
125             is_bcm = 1;
126 #else
127 #endif
128             break;
129         case COMPANY_ID_ST_MICROELECTRONICS:
130             printf("ST Microelectronics - using STLC2500d driver.\n");
131             use_fast_uart();
132             hci_set_chipset(btstack_chipset_stlc2500d_instance());
133             break;
134         case COMPANY_ID_EM_MICROELECTRONICS_MARIN:
135             printf("EM Microelectronics - using EM9301 driver.\n");
136             hci_set_chipset(btstack_chipset_em9301_instance());
137             break;
138         case COMPANY_ID_NORDIC_SEMICONDUCTOR_ASA:
139             printf("Nordic Semiconductor nRF5 chipset.\n");
140             break;
141         default:
142             printf("Unknown manufacturer / manufacturer not supported yet.\n");
143             break;
144     }
145 }
146 
147 int main(int argc, const char * argv[]){
148 	printf("BTstack on windows booting up\n");
149 
150 	/// GET STARTED with BTstack ///
151 	btstack_memory_init();
152     btstack_run_loop_init(btstack_run_loop_windows_get_instance());
153 
154     hci_dump_open(NULL, HCI_DUMP_STDOUT);
155 
156     // pick serial port
157     config.device_name = "\\\\.\\COM7";
158 
159     // init HCI
160     const btstack_uart_block_t * uart_driver = btstack_uart_block_windows_instance();
161 	const hci_transport_t * transport = hci_transport_h4_instance(uart_driver);
162     const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance();
163 	hci_init(transport, (void*) &config);
164     hci_set_link_key_db(link_key_db);
165 
166     // inform about BTstack state
167     hci_event_callback_registration.callback = &packet_handler;
168     hci_add_event_handler(&hci_event_callback_registration);
169 
170     // setup dynamic chipset driver setup
171     hci_set_local_version_information_callback(&local_version_information_callback);
172 
173     // handle CTRL-c
174     signal(SIGINT, sigint_handler);
175 
176     // setup app
177     btstack_main(argc, argv);
178 
179     // go
180     btstack_run_loop_execute();
181 
182 	return 0;
183 }
184