xref: /btstack/port/windows-h4/main.c (revision c7e2dea5df4b1c023086abba9220641cae703cd2)
14e630824SMatthias Ringwald #include <stdint.h>
24e630824SMatthias Ringwald #include <stdio.h>
34e630824SMatthias Ringwald #include <stdlib.h>
44e630824SMatthias Ringwald #include <string.h>
54e630824SMatthias Ringwald #include <signal.h>
64e630824SMatthias Ringwald 
74e630824SMatthias Ringwald #include "btstack_config.h"
84e630824SMatthias Ringwald 
94e630824SMatthias Ringwald #include "btstack_debug.h"
104e630824SMatthias Ringwald #include "btstack_event.h"
114e630824SMatthias Ringwald #include "btstack_memory.h"
124e630824SMatthias Ringwald #include "btstack_run_loop.h"
134e630824SMatthias Ringwald #include "btstack_run_loop_windows.h"
144e630824SMatthias Ringwald #include "hci.h"
154e630824SMatthias Ringwald #include "hci_dump.h"
164e630824SMatthias Ringwald #include "hal_led.h"
17422979b1SMatthias Ringwald #include "btstack_link_key_db_fs.h"
18422979b1SMatthias Ringwald 
194e630824SMatthias Ringwald // #include "stdin_support.h"
204e630824SMatthias Ringwald 
21422979b1SMatthias Ringwald #include "btstack_chipset_bcm.h"
22422979b1SMatthias Ringwald #include "btstack_chipset_csr.h"
23422979b1SMatthias Ringwald #include "btstack_chipset_cc256x.h"
24422979b1SMatthias Ringwald #include "btstack_chipset_em9301.h"
25422979b1SMatthias Ringwald #include "btstack_chipset_stlc2500d.h"
26422979b1SMatthias Ringwald #include "btstack_chipset_tc3566x.h"
27422979b1SMatthias Ringwald 
2827ff675bSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
2927ff675bSMatthias Ringwald 
30422979b1SMatthias Ringwald static hci_transport_config_uart_t config = {
31422979b1SMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
32422979b1SMatthias Ringwald     115200,
33422979b1SMatthias Ringwald     0,  // main baudrate
34422979b1SMatthias Ringwald     1,  // flow control
35422979b1SMatthias Ringwald     NULL,
36422979b1SMatthias Ringwald };
37422979b1SMatthias Ringwald 
38422979b1SMatthias Ringwald int is_bcm;
39422979b1SMatthias Ringwald 
404e630824SMatthias Ringwald static int led_state = 0;
41422979b1SMatthias Ringwald 
424e630824SMatthias Ringwald void hal_led_toggle(void){
434e630824SMatthias Ringwald     led_state = 1 - led_state;
444e630824SMatthias Ringwald     printf("LED State %u\n", led_state);
454e630824SMatthias Ringwald }
464e630824SMatthias Ringwald 
4727ff675bSMatthias Ringwald static void sigint_handler(int param){
4827ff675bSMatthias Ringwald 
4927ff675bSMatthias Ringwald #ifndef _WIN32
5027ff675bSMatthias Ringwald     // reset anyway
5127ff675bSMatthias Ringwald     btstack_stdin_reset();
5227ff675bSMatthias Ringwald #endif
5327ff675bSMatthias Ringwald     log_info(" <= SIGINT received, shutting down..\n");
5427ff675bSMatthias Ringwald     // hci_power_control(HCI_POWER_OFF);
5527ff675bSMatthias Ringwald     // hci_close();
5627ff675bSMatthias Ringwald     log_info("Good bye, see you.\n");
5727ff675bSMatthias Ringwald     exit(0);
5827ff675bSMatthias Ringwald }
5927ff675bSMatthias Ringwald 
60422979b1SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
61422979b1SMatthias Ringwald 
62422979b1SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
63422979b1SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
64422979b1SMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
65422979b1SMatthias Ringwald         case BTSTACK_EVENT_STATE:
66422979b1SMatthias Ringwald             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
67422979b1SMatthias Ringwald             printf("BTstack up and running.\n");
68422979b1SMatthias Ringwald             break;
69422979b1SMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
70422979b1SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
71422979b1SMatthias Ringwald                 // terminate, name 248 chars
72422979b1SMatthias Ringwald                 packet[6+248] = 0;
73422979b1SMatthias Ringwald                 printf("Local name: %s\n", &packet[6]);
74422979b1SMatthias Ringwald #if 0
75422979b1SMatthias Ringwald                 if (is_bcm){
76422979b1SMatthias Ringwald                     btstack_chipset_bcm_set_device_name((const char *)&packet[6]);
77422979b1SMatthias Ringwald                 }
78422979b1SMatthias Ringwald #endif
79422979b1SMatthias Ringwald             }
80422979b1SMatthias Ringwald             break;
81422979b1SMatthias Ringwald         default:
82422979b1SMatthias Ringwald             break;
83422979b1SMatthias Ringwald     }
84422979b1SMatthias Ringwald }
85422979b1SMatthias Ringwald 
86422979b1SMatthias Ringwald static void use_fast_uart(void){
87422979b1SMatthias Ringwald     printf("Using 921600 baud.\n");
88422979b1SMatthias Ringwald     config.baudrate_main = 921600;
89422979b1SMatthias Ringwald }
90422979b1SMatthias Ringwald 
91422979b1SMatthias Ringwald static void local_version_information_callback(uint8_t * packet){
92422979b1SMatthias Ringwald     printf("Local version information:\n");
93422979b1SMatthias Ringwald     uint16_t hci_version    = little_endian_read_16(packet, 4);
94422979b1SMatthias Ringwald     uint16_t hci_revision   = little_endian_read_16(packet, 6);
95422979b1SMatthias Ringwald     uint16_t lmp_version    = little_endian_read_16(packet, 8);
96422979b1SMatthias Ringwald     uint16_t manufacturer   = little_endian_read_16(packet, 10);
97422979b1SMatthias Ringwald     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
98422979b1SMatthias Ringwald     printf("- HCI Version  0x%04x\n", hci_version);
99422979b1SMatthias Ringwald     printf("- HCI Revision 0x%04x\n", hci_revision);
100422979b1SMatthias Ringwald     printf("- LMP Version  0x%04x\n", lmp_version);
101422979b1SMatthias Ringwald     printf("- LMP Revision 0x%04x\n", lmp_subversion);
102422979b1SMatthias Ringwald     printf("- Manufacturer 0x%04x\n", manufacturer);
103422979b1SMatthias Ringwald     switch (manufacturer){
104422979b1SMatthias Ringwald         case COMPANY_ID_CAMBRIDGE_SILICON_RADIO:
105422979b1SMatthias Ringwald             printf("Cambridge Silicon Radio - CSR chipset.\n");
106422979b1SMatthias Ringwald             use_fast_uart();
107422979b1SMatthias Ringwald             hci_set_chipset(btstack_chipset_csr_instance());
108422979b1SMatthias Ringwald             break;
109422979b1SMatthias Ringwald         case COMPANY_ID_TEXAS_INSTRUMENTS_INC:
110422979b1SMatthias Ringwald             printf("Texas Instruments - CC256x compatible chipset.\n");
111*c7e2dea5SMatthias Ringwald             if (lmp_subversion != btstack_chipset_cc256x_lmp_subversion()){
112*c7e2dea5SMatthias Ringwald                 printf("Error: LMP Subversion does not match initscript!");
113*c7e2dea5SMatthias Ringwald                 printf("Your initscripts is for %s chipset\n", btstack_chipset_cc256x_lmp_subversion() < lmp_subversion ? "an older" : "a newer");
114*c7e2dea5SMatthias Ringwald                 printf("Please update Makefile to include the appropriate bluetooth_init_cc256???.c file\n");
115*c7e2dea5SMatthias Ringwald                 exit(10);
116*c7e2dea5SMatthias Ringwald             }
117422979b1SMatthias Ringwald             use_fast_uart();
118422979b1SMatthias Ringwald             hci_set_chipset(btstack_chipset_cc256x_instance());
119422979b1SMatthias Ringwald #ifdef ENABLE_EHCILL
120422979b1SMatthias Ringwald             printf("eHCILL enabled.\n");
121422979b1SMatthias Ringwald #else
122422979b1SMatthias Ringwald             printf("eHCILL disable.\n");
123422979b1SMatthias Ringwald #endif
124422979b1SMatthias Ringwald             break;
125422979b1SMatthias Ringwald         case COMPANY_ID_BROADCOM_CORPORATION:
126422979b1SMatthias Ringwald             printf("Broadcom - not supported on Windows yet.\n");
127422979b1SMatthias Ringwald #if 0
128422979b1SMatthias Ringwald             printf("Broadcom - using BCM driver.\n");
129422979b1SMatthias Ringwald             hci_set_chipset(btstack_chipset_bcm_instance());
130422979b1SMatthias Ringwald             use_fast_uart();
131422979b1SMatthias Ringwald             is_bcm = 1;
132422979b1SMatthias Ringwald #else
133422979b1SMatthias Ringwald #endif
134422979b1SMatthias Ringwald             break;
135422979b1SMatthias Ringwald         case COMPANY_ID_ST_MICROELECTRONICS:
136422979b1SMatthias Ringwald             printf("ST Microelectronics - using STLC2500d driver.\n");
137422979b1SMatthias Ringwald             use_fast_uart();
138422979b1SMatthias Ringwald             hci_set_chipset(btstack_chipset_stlc2500d_instance());
139422979b1SMatthias Ringwald             break;
140422979b1SMatthias Ringwald         case COMPANY_ID_EM_MICROELECTRONICS_MARIN:
141422979b1SMatthias Ringwald             printf("EM Microelectronics - using EM9301 driver.\n");
142422979b1SMatthias Ringwald             hci_set_chipset(btstack_chipset_em9301_instance());
143422979b1SMatthias Ringwald             break;
144422979b1SMatthias Ringwald         case COMPANY_ID_NORDIC_SEMICONDUCTOR_ASA:
145422979b1SMatthias Ringwald             printf("Nordic Semiconductor nRF5 chipset.\n");
146422979b1SMatthias Ringwald             break;
147422979b1SMatthias Ringwald         default:
148422979b1SMatthias Ringwald             printf("Unknown manufacturer / manufacturer not supported yet.\n");
149422979b1SMatthias Ringwald             break;
150422979b1SMatthias Ringwald     }
151422979b1SMatthias Ringwald }
152422979b1SMatthias Ringwald 
15327ff675bSMatthias Ringwald int main(int argc, const char * argv[]){
1544e630824SMatthias Ringwald 	printf("BTstack on windows booting up\n");
1554e630824SMatthias Ringwald 
1564e630824SMatthias Ringwald 	/// GET STARTED with BTstack ///
1574e630824SMatthias Ringwald 	btstack_memory_init();
1584e630824SMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_windows_get_instance());
1594e630824SMatthias Ringwald 
160422979b1SMatthias Ringwald     hci_dump_open(NULL, HCI_DUMP_STDOUT);
161422979b1SMatthias Ringwald 
162422979b1SMatthias Ringwald     // pick serial port
163422979b1SMatthias Ringwald     config.device_name = "\\\\.\\COM7";
164422979b1SMatthias Ringwald 
165422979b1SMatthias Ringwald     // init HCI
166422979b1SMatthias Ringwald     const btstack_uart_block_t * uart_driver = btstack_uart_block_windows_instance();
167422979b1SMatthias Ringwald 	const hci_transport_t * transport = hci_transport_h4_instance(uart_driver);
168422979b1SMatthias Ringwald     const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance();
169422979b1SMatthias Ringwald 	hci_init(transport, (void*) &config);
170422979b1SMatthias Ringwald     hci_set_link_key_db(link_key_db);
171422979b1SMatthias Ringwald 
172422979b1SMatthias Ringwald     // inform about BTstack state
173422979b1SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
174422979b1SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
175422979b1SMatthias Ringwald 
176422979b1SMatthias Ringwald     // setup dynamic chipset driver setup
177422979b1SMatthias Ringwald     hci_set_local_version_information_callback(&local_version_information_callback);
178422979b1SMatthias Ringwald 
17927ff675bSMatthias Ringwald     // handle CTRL-c
18027ff675bSMatthias Ringwald     signal(SIGINT, sigint_handler);
18127ff675bSMatthias Ringwald 
18227ff675bSMatthias Ringwald     // setup app
18327ff675bSMatthias Ringwald     btstack_main(argc, argv);
18427ff675bSMatthias Ringwald 
18527ff675bSMatthias Ringwald     // go
18627ff675bSMatthias Ringwald     btstack_run_loop_execute();
18727ff675bSMatthias Ringwald 
1884e630824SMatthias Ringwald 	return 0;
1894e630824SMatthias Ringwald }
190