xref: /btstack/port/windows-h4/main.c (revision 5096a6aec9c7814a3c50c0f6806f1c64a4d9e863)
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     // reset even if not setup before
50     btstack_stdin_reset();
51 
52     log_info(" <= SIGINT received, shutting down..\n");
53     // hci_power_control(HCI_POWER_OFF);
54     // hci_close();
55     log_info("Good bye, see you.\n");
56     exit(0);
57 }
58 
59 static btstack_packet_callback_registration_t hci_event_callback_registration;
60 
61 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
62     if (packet_type != HCI_EVENT_PACKET) return;
63     switch (hci_event_packet_get_type(packet)){
64         case BTSTACK_EVENT_STATE:
65             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
66             printf("BTstack up and running.\n");
67             break;
68         case HCI_EVENT_COMMAND_COMPLETE:
69             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
70                 // terminate, name 248 chars
71                 packet[6+248] = 0;
72                 printf("Local name: %s\n", &packet[6]);
73                 if (is_bcm){
74                     btstack_chipset_bcm_set_device_name((const char *)&packet[6]);
75                 }
76             }
77             break;
78         default:
79             break;
80     }
81 }
82 
83 static void use_fast_uart(void){
84     printf("Using 921600 baud.\n");
85     config.baudrate_main = 921600;
86 }
87 
88 static void local_version_information_callback(uint8_t * packet){
89     printf("Local version information:\n");
90     uint16_t hci_version    = little_endian_read_16(packet, 4);
91     uint16_t hci_revision   = little_endian_read_16(packet, 6);
92     uint16_t lmp_version    = little_endian_read_16(packet, 8);
93     uint16_t manufacturer   = little_endian_read_16(packet, 10);
94     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
95     printf("- HCI Version  0x%04x\n", hci_version);
96     printf("- HCI Revision 0x%04x\n", hci_revision);
97     printf("- LMP Version  0x%04x\n", lmp_version);
98     printf("- LMP Revision 0x%04x\n", lmp_subversion);
99     printf("- Manufacturer 0x%04x\n", manufacturer);
100     switch (manufacturer){
101         case COMPANY_ID_CAMBRIDGE_SILICON_RADIO:
102             printf("Cambridge Silicon Radio - CSR chipset.\n");
103             use_fast_uart();
104             hci_set_chipset(btstack_chipset_csr_instance());
105             break;
106         case COMPANY_ID_TEXAS_INSTRUMENTS_INC:
107             printf("Texas Instruments - CC256x compatible chipset.\n");
108             use_fast_uart();
109             hci_set_chipset(btstack_chipset_cc256x_instance());
110 #ifdef ENABLE_EHCILL
111             printf("eHCILL enabled.\n");
112 #else
113             printf("eHCILL disable.\n");
114 #endif
115             break;
116         case COMPANY_ID_BROADCOM_CORPORATION:
117             printf("Broadcom - using BCM driver.\n");
118             hci_set_chipset(btstack_chipset_bcm_instance());
119             use_fast_uart();
120             is_bcm = 1;
121             break;
122         case COMPANY_ID_ST_MICROELECTRONICS:
123             printf("ST Microelectronics - using STLC2500d driver.\n");
124             use_fast_uart();
125             hci_set_chipset(btstack_chipset_stlc2500d_instance());
126             break;
127         case COMPANY_ID_EM_MICROELECTRONICS_MARIN:
128             printf("EM Microelectronics - using EM9301 driver.\n");
129             hci_set_chipset(btstack_chipset_em9301_instance());
130             break;
131         case COMPANY_ID_NORDIC_SEMICONDUCTOR_ASA:
132             printf("Nordic Semiconductor nRF5 chipset.\n");
133             break;
134         default:
135             printf("Unknown manufacturer / manufacturer not supported yet.\n");
136             break;
137     }
138 }
139 
140 int main(int argc, const char * argv[]){
141 	printf("BTstack on windows booting up\n");
142 
143 	/// GET STARTED with BTstack ///
144 	btstack_memory_init();
145     btstack_run_loop_init(btstack_run_loop_windows_get_instance());
146 
147     hci_dump_open("hci_dump.pklg", HCI_DUMP_PACKETLOGGER);
148 
149     // pick serial port
150     config.device_name = "\\\\.\\COM7";
151 
152     // init HCI
153     const btstack_uart_block_t * uart_driver = btstack_uart_block_windows_instance();
154 	const hci_transport_t * transport = hci_transport_h4_instance(uart_driver);
155     const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance();
156 	hci_init(transport, (void*) &config);
157     hci_set_link_key_db(link_key_db);
158 
159     // inform about BTstack state
160     hci_event_callback_registration.callback = &packet_handler;
161     hci_add_event_handler(&hci_event_callback_registration);
162 
163     // setup dynamic chipset driver setup
164     hci_set_local_version_information_callback(&local_version_information_callback);
165 
166     // handle CTRL-c
167     signal(SIGINT, sigint_handler);
168 
169     // setup app
170     btstack_main(argc, argv);
171 
172     // go
173     btstack_run_loop_execute();
174 
175 	return 0;
176 }
177