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