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