windows: use btstack_stdin_window_register_ctrl_c_callback to trigger shutdown on ctrl-c

This commit is contained in:
Matthias Ringwald 2021-07-05 10:38:00 +02:00
parent de9c9f72d7
commit d5b02eade8
5 changed files with 200 additions and 171 deletions

View File

@ -35,7 +35,7 @@
* *
*/ */
#define __BTSTACK_FILE__ "main.c" #define BTSTACK_FILE__ "main.c"
// ***************************************************************************** // *****************************************************************************
// //
@ -47,26 +47,26 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <signal.h>
#include "btstack_config.h" #include "btstack_config.h"
#include "ble/le_device_db_tlv.h"
#include "btstack_chipset_da14581.h"
#include "btstack_debug.h" #include "btstack_debug.h"
#include "btstack_event.h" #include "btstack_event.h"
#include "btstack_memory.h" #include "btstack_memory.h"
#include "btstack_run_loop.h" #include "btstack_run_loop.h"
#include "btstack_run_loop_windows.h" #include "btstack_run_loop_windows.h"
#include "ble/le_device_db_tlv.h" #include "btstack_stdin.h"
#include "btstack_stdin_windows.h"
#include "btstack_tlv_posix.h"
#include "hal_led.h"
#include "hci.h" #include "hci.h"
#include "hci_585.h"
#include "hci_dump.h" #include "hci_dump.h"
#include "hci_dump_posix_fs.h" #include "hci_dump_posix_fs.h"
#include "hci_transport.h" #include "hci_transport.h"
#include "hci_transport_h4.h" #include "hci_transport_h4.h"
#include "btstack_stdin.h"
#include "btstack_tlv_posix.h"
#include "hal_led.h"
#include "btstack_chipset_da14581.h"
#include "hci_585.h"
static int main_argc; static int main_argc;
static const char ** main_argv; static const char ** main_argv;
@ -78,6 +78,8 @@ static btstack_uart_config_t uart_config;
static char tlv_db_path[100]; static char tlv_db_path[100];
static const btstack_tlv_t * tlv_impl; static const btstack_tlv_t * tlv_impl;
static btstack_tlv_posix_t tlv_context; static btstack_tlv_posix_t tlv_context;
static bd_addr_t local_addr;
static bool shutdown_triggered;
int btstack_main(int argc, const char * argv[]); int btstack_main(int argc, const char * argv[]);
@ -92,40 +94,47 @@ static hci_transport_config_uart_t transport_config = {
static btstack_packet_callback_registration_t hci_event_callback_registration; static btstack_packet_callback_registration_t hci_event_callback_registration;
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
bd_addr_t addr;
if (packet_type != HCI_EVENT_PACKET) return; if (packet_type != HCI_EVENT_PACKET) return;
switch (hci_event_packet_get_type(packet)){ switch (hci_event_packet_get_type(packet)){
case BTSTACK_EVENT_STATE: case BTSTACK_EVENT_STATE:
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; switch (btstack_event_state_get_state(packet)){
gap_local_bd_addr(addr); case HCI_STATE_WORKING:
printf("BTstack up and running at %s\n", bd_addr_to_str(addr)); gap_local_bd_addr(local_addr);
// setup TLV printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
strcat(tlv_db_path, bd_addr_to_str(addr)); strcat(tlv_db_path, bd_addr_to_str(local_addr));
strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
btstack_tlv_set_instance(tlv_impl, &tlv_context); btstack_tlv_set_instance(tlv_impl, &tlv_context);
le_device_db_tlv_configure(tlv_impl, &tlv_context); #ifdef ENABLE_CLASSIC
hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
#endif
#ifdef ENABLE_BLE
le_device_db_tlv_configure(tlv_impl, &tlv_context);
#endif
break;
case HCI_STATE_OFF:
btstack_tlv_posix_deinit(&tlv_context);
if (!shutdown_triggered) break;
// reset stdin
btstack_stdin_reset();
log_info("Good bye, see you.\n");
exit(0);
break;
default:
break;
}
break; break;
default: default:
break; break;
} }
} }
static void sigint_handler(int param){ static void trigger_shutdown(void){
UNUSED(param);
printf("CTRL-C - SIGINT received, shutting down..\n"); printf("CTRL-C - SIGINT received, shutting down..\n");
log_info("sigint_handler: shutting down"); log_info("sigint_handler: shutting down");
shutdown_triggered = true;
// reset anyway
btstack_stdin_reset();
// power down
hci_power_control(HCI_POWER_OFF); hci_power_control(HCI_POWER_OFF);
hci_close();
log_info("Good bye, see you.\n");
exit(0);
} }
static int led_state = 0; static int led_state = 0;
@ -151,8 +160,9 @@ static void phase2(int status){
hci_event_callback_registration.callback = &packet_handler; hci_event_callback_registration.callback = &packet_handler;
hci_add_event_handler(&hci_event_callback_registration); hci_add_event_handler(&hci_event_callback_registration);
// handle CTRL-c // setup stdin to handle CTRL-c
signal(SIGINT, sigint_handler); btstack_stdin_windows_init();
btstack_stdin_window_register_ctrl_c_callback(&trigger_shutdown);
// setup app // setup app
btstack_main(main_argc, main_argv); btstack_main(main_argc, main_argv);

View File

@ -35,7 +35,7 @@
* *
*/ */
#define __BTSTACK_FILE__ "main.c" #define BTSTACK_FILE__ "main.c"
// ***************************************************************************** // *****************************************************************************
// //
@ -47,26 +47,26 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <signal.h>
#include "btstack_config.h" #include "btstack_config.h"
#include "ble/le_device_db_tlv.h"
#include "bluetooth_company_id.h"
#include "btstack_chipset_zephyr.h"
#include "btstack_debug.h" #include "btstack_debug.h"
#include "btstack_event.h" #include "btstack_event.h"
#include "btstack_memory.h" #include "btstack_memory.h"
#include "btstack_run_loop.h" #include "btstack_run_loop.h"
#include "btstack_run_loop_windows.h" #include "btstack_run_loop_windows.h"
#include "bluetooth_company_id.h" #include "btstack_stdin.h"
#include "btstack_stdin_windows.h"
#include "btstack_tlv_posix.h"
#include "hal_led.h"
#include "hci.h" #include "hci.h"
#include "hci_dump.h" #include "hci_dump.h"
#include "hci_dump_posix_fs.h" #include "hci_dump_posix_fs.h"
#include "hci_transport.h" #include "hci_transport.h"
#include "hci_transport_h4.h" #include "hci_transport_h4.h"
#include "btstack_stdin.h"
#include "btstack_chipset_zephyr.h"
#include "hal_led.h"
#include "btstack_tlv_posix.h"
#include "ble/le_device_db_tlv.h"
int btstack_main(int argc, const char * argv[]); int btstack_main(int argc, const char * argv[]);
@ -88,27 +88,40 @@ static bd_addr_t static_address;
static char tlv_db_path[100]; static char tlv_db_path[100];
static const btstack_tlv_t * tlv_impl; static const btstack_tlv_t * tlv_impl;
static btstack_tlv_posix_t tlv_context; static btstack_tlv_posix_t tlv_context;
static bool shutdown_triggered;
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
bd_addr_t addr;
if (packet_type != HCI_EVENT_PACKET) return; if (packet_type != HCI_EVENT_PACKET) return;
switch (hci_event_packet_get_type(packet)){ switch (hci_event_packet_get_type(packet)){
case BTSTACK_EVENT_STATE: case BTSTACK_EVENT_STATE:
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; switch (btstack_event_state_get_state(packet)){
printf("BTstack up and running as %s\n", bd_addr_to_str(static_address)); case HCI_STATE_WORKING:
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); printf("BTstack up and running as %s\n", bd_addr_to_str(static_address));
strcat(tlv_db_path, bd_addr_to_str(static_address)); strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); strcat(tlv_db_path, bd_addr_to_str(static_address));
tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
btstack_tlv_set_instance(tlv_impl, &tlv_context); tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
btstack_tlv_set_instance(tlv_impl, &tlv_context);
#ifdef ENABLE_BLE #ifdef ENABLE_BLE
le_device_db_tlv_configure(tlv_impl, &tlv_context); le_device_db_tlv_configure(tlv_impl, &tlv_context);
#endif #endif
break;
case HCI_STATE_OFF:
btstack_tlv_posix_deinit(&tlv_context);
if (!shutdown_triggered) break;
// reset stdin
btstack_stdin_reset();
log_info("Good bye, see you.\n");
exit(0);
break;
default:
break;
}
break; break;
case HCI_EVENT_COMMAND_COMPLETE: case HCI_EVENT_COMMAND_COMPLETE:
if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0){ if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0){
reverse_48(&packet[7], static_address); reverse_48(&packet[7], static_address);
gap_random_address_set(addr); gap_random_address_set(static_address);
} }
break; break;
default: default:
@ -116,20 +129,11 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
} }
} }
static void sigint_handler(int param){ static void trigger_shutdown(void){
UNUSED(param);
printf("CTRL-C - SIGINT received, shutting down..\n"); printf("CTRL-C - SIGINT received, shutting down..\n");
log_info("sigint_handler: shutting down"); log_info("sigint_handler: shutting down");
shutdown_triggered = true;
// reset anyway
btstack_stdin_reset();
// power down
hci_power_control(HCI_POWER_OFF); hci_power_control(HCI_POWER_OFF);
hci_close();
log_info("Good bye, see you.\n");
exit(0);
} }
static int led_state = 0; static int led_state = 0;
@ -172,8 +176,9 @@ int main(int argc, const char * argv[]){
hci_event_callback_registration.callback = &packet_handler; hci_event_callback_registration.callback = &packet_handler;
hci_add_event_handler(&hci_event_callback_registration); hci_add_event_handler(&hci_event_callback_registration);
// handle CTRL-c // setup stdin to handle CTRL-c
signal(SIGINT, sigint_handler); btstack_stdin_windows_init();
btstack_stdin_window_register_ctrl_c_callback(&trigger_shutdown);
// setup app // setup app
btstack_main(argc, argv); btstack_main(argc, argv);

View File

@ -35,50 +35,48 @@
* *
*/ */
#define __BTSTACK_FILE__ "main.c" #define BTSTACK_FILE__ "main.c"
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <signal.h>
#include "btstack_config.h" #include "btstack_config.h"
#include "bluetooth_company_id.h"
#include "ble/le_device_db_tlv.h"
#include "bluetooth_company_id.h"
#include "btstack_chipset_bcm.h"
#include "btstack_chipset_cc256x.h"
#include "btstack_chipset_csr.h"
#include "btstack_chipset_em9301.h"
#include "btstack_chipset_stlc2500d.h"
#include "btstack_chipset_tc3566x.h"
#include "btstack_debug.h" #include "btstack_debug.h"
#include "btstack_event.h" #include "btstack_event.h"
#include "btstack_memory.h" #include "btstack_memory.h"
#include "btstack_run_loop.h" #include "btstack_run_loop.h"
#include "btstack_run_loop_windows.h" #include "btstack_run_loop_windows.h"
#include "btstack_stdin.h"
#include "btstack_stdin_windows.h"
#include "btstack_tlv_posix.h"
#include "classic/btstack_link_key_db_tlv.h"
#include "hal_led.h"
#include "hci.h" #include "hci.h"
#include "hci_dump.h" #include "hci_dump.h"
#include "hci_dump_posix_fs.h" #include "hci_dump_posix_fs.h"
#include "hci_transport.h" #include "hci_transport.h"
#include "hci_transport_h4.h" #include "hci_transport_h4.h"
#include "hal_led.h"
#include "btstack_tlv_posix.h"
#include "ble/le_device_db_tlv.h"
#include "classic/btstack_link_key_db_tlv.h"
#include "btstack_stdin.h"
#include "btstack_chipset_bcm.h"
#include "btstack_chipset_csr.h"
#include "btstack_chipset_cc256x.h"
#include "btstack_chipset_em9301.h"
#include "btstack_chipset_stlc2500d.h"
#include "btstack_chipset_tc3566x.h"
int btstack_main(int argc, const char * argv[]); int btstack_main(int argc, const char * argv[]);
static void local_version_information_handler(uint8_t * packet); static void local_version_information_handler(uint8_t * packet);
static hci_transport_config_uart_t config = { static hci_transport_config_uart_t config = {
HCI_TRANSPORT_CONFIG_UART, HCI_TRANSPORT_CONFIG_UART,
115200, 115200,
0, // main baudrate 0, // main baudrate
1, // flow control 1, // flow control
NULL, NULL,
}; };
int is_bcm; int is_bcm;
@ -91,50 +89,53 @@ static char tlv_db_path[100];
static const btstack_tlv_t * tlv_impl; static const btstack_tlv_t * tlv_impl;
static btstack_tlv_posix_t tlv_context; static btstack_tlv_posix_t tlv_context;
static bd_addr_t local_addr; static bd_addr_t local_addr;
static bool shutdown_triggered;
void hal_led_toggle(void){ void hal_led_toggle(void){
led_state = 1 - led_state; led_state = 1 - led_state;
printf("LED State %u\n", led_state); printf("LED State %u\n", led_state);
} }
static void sigint_handler(int param){ static void trigger_shutdown(void){
UNUSED(param); printf("CTRL-C - SIGINT received, shutting down..\n");
printf("CTRL-C = SIGINT received, shutting down..\n");
log_info("sigint_handler: shutting down"); log_info("sigint_handler: shutting down");
shutdown_triggered = true;
// reset anyway
btstack_stdin_reset();
// power down
hci_power_control(HCI_POWER_OFF); hci_power_control(HCI_POWER_OFF);
hci_close();
log_info("Good bye, see you.\n");
exit(0);
} }
static btstack_packet_callback_registration_t hci_event_callback_registration; static btstack_packet_callback_registration_t hci_event_callback_registration;
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
bd_addr_t addr;
if (packet_type != HCI_EVENT_PACKET) return; if (packet_type != HCI_EVENT_PACKET) return;
switch (hci_event_packet_get_type(packet)){ switch (hci_event_packet_get_type(packet)){
case BTSTACK_EVENT_STATE: case BTSTACK_EVENT_STATE:
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; switch (btstack_event_state_get_state(packet)){
gap_local_bd_addr(addr); case HCI_STATE_WORKING:
printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); gap_local_bd_addr(local_addr);
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
strcat(tlv_db_path, bd_addr_to_str(local_addr)); strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); strcat(tlv_db_path, bd_addr_to_str(local_addr));
tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
btstack_tlv_set_instance(tlv_impl, &tlv_context); tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
btstack_tlv_set_instance(tlv_impl, &tlv_context);
#ifdef ENABLE_CLASSIC #ifdef ENABLE_CLASSIC
hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
#endif #endif
#ifdef ENABLE_BLE #ifdef ENABLE_BLE
le_device_db_tlv_configure(tlv_impl, &tlv_context); le_device_db_tlv_configure(tlv_impl, &tlv_context);
#endif #endif
printf("BTstack up and running at %s\n", bd_addr_to_str(addr)); break;
case HCI_STATE_OFF:
btstack_tlv_posix_deinit(&tlv_context);
if (!shutdown_triggered) break;
// reset stdin
btstack_stdin_reset();
log_info("Good bye, see you.\n");
exit(0);
break;
default:
break;
}
break; break;
case HCI_EVENT_COMMAND_COMPLETE: case HCI_EVENT_COMMAND_COMPLETE:
if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){ if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
@ -256,8 +257,9 @@ int main(int argc, const char * argv[]){
hci_event_callback_registration.callback = &packet_handler; hci_event_callback_registration.callback = &packet_handler;
hci_add_event_handler(&hci_event_callback_registration); hci_add_event_handler(&hci_event_callback_registration);
// handle CTRL-c // setup stdin to handle CTRL-c
signal(SIGINT, sigint_handler); btstack_stdin_windows_init();
btstack_stdin_window_register_ctrl_c_callback(&trigger_shutdown);
// setup app // setup app
btstack_main(argc, argv); btstack_main(argc, argv);

View File

@ -35,7 +35,7 @@
* *
*/ */
#define __BTSTACK_FILE__ "main.c" #define BTSTACK_FILE__ "main.c"
// ***************************************************************************** // *****************************************************************************
// //
@ -47,26 +47,26 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <signal.h>
#include "btstack_config.h" #include "btstack_config.h"
#include "ble/le_device_db_tlv.h"
#include "btstack_chipset_intel_firmware.h"
#include "btstack_debug.h" #include "btstack_debug.h"
#include "btstack_event.h" #include "btstack_event.h"
#include "btstack_memory.h" #include "btstack_memory.h"
#include "btstack_run_loop.h" #include "btstack_run_loop.h"
#include "btstack_run_loop_windows.h" #include "btstack_run_loop_windows.h"
#include "btstack_stdin.h"
#include "btstack_stdin_windows.h"
#include "btstack_tlv_posix.h" #include "btstack_tlv_posix.h"
#include "ble/le_device_db_tlv.h"
#include "classic/btstack_link_key_db_tlv.h" #include "classic/btstack_link_key_db_tlv.h"
#include "hal_led.h" #include "hal_led.h"
#include "hci.h" #include "hci.h"
#include "hci_dump.h" #include "hci_dump.h"
#include "hci_dump_posix_fs.h" #include "hci_dump_posix_fs.h"
#include "hci_transport.h" #include "hci_transport.h"
#include "hci_transport_h4.h" #include "hci_transport_usb.h"
#include "btstack_stdin.h"
#include "btstack_chipset_intel_firmware.h"
int btstack_main(int argc, const char * argv[]); int btstack_main(int argc, const char * argv[]);
@ -83,41 +83,45 @@ static int main_argc;
static const char ** main_argv; static const char ** main_argv;
static const hci_transport_t * transport; static const hci_transport_t * transport;
static int intel_firmware_loaded; static int intel_firmware_loaded;
static bool shutdown_triggered;
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
if (packet_type != HCI_EVENT_PACKET) return; if (packet_type != HCI_EVENT_PACKET) return;
if (hci_event_packet_get_type(packet) != BTSTACK_EVENT_STATE) return; if (hci_event_packet_get_type(packet) != BTSTACK_EVENT_STATE) return;
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; switch (btstack_event_state_get_state(packet)){
gap_local_bd_addr(local_addr); case HCI_STATE_WORKING:
printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); gap_local_bd_addr(local_addr);
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
strcat(tlv_db_path, bd_addr_to_str(local_addr)); strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); strcat(tlv_db_path, bd_addr_to_str(local_addr));
tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
btstack_tlv_set_instance(tlv_impl, &tlv_context); tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
btstack_tlv_set_instance(tlv_impl, &tlv_context);
#ifdef ENABLE_CLASSIC #ifdef ENABLE_CLASSIC
hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
#endif #endif
#ifdef ENABLE_BLE #ifdef ENABLE_BLE
le_device_db_tlv_configure(tlv_impl, &tlv_context); le_device_db_tlv_configure(tlv_impl, &tlv_context);
#endif #endif
break;
case HCI_STATE_OFF:
btstack_tlv_posix_deinit(&tlv_context);
if (!shutdown_triggered) break;
// reset stdin
btstack_stdin_reset();
log_info("Good bye, see you.\n");
exit(0);
break;
default:
break;
}
} }
static void sigint_handler(int param){ static void trigger_shutdown(void){
UNUSED(param); printf("CTRL-C - SIGINT received, shutting down..\n");
printf("CTRL-C - SIGINT received, shutting down..\n");
log_info("sigint_handler: shutting down"); log_info("sigint_handler: shutting down");
shutdown_triggered = true;
// reset anyway hci_power_control(HCI_POWER_OFF);
btstack_stdin_reset();
// power off and close only if hci was initialized before
if (intel_firmware_loaded){
hci_power_control( HCI_POWER_OFF);
hci_close();
}
exit(0);
} }
static int led_state = 0; static int led_state = 0;
@ -155,8 +159,8 @@ int main(int argc, const char * argv[]){
printf("BTstack/windows-winusb booting up\n"); printf("BTstack/windows-winusb booting up\n");
/// GET STARTED with BTstack /// /// GET STARTED with BTstack ///
btstack_memory_init(); btstack_memory_init();
btstack_run_loop_init(btstack_run_loop_windows_get_instance()); btstack_run_loop_init(btstack_run_loop_windows_get_instance());
// log into file using HCI_DUMP_PACKETLOGGER format // log into file using HCI_DUMP_PACKETLOGGER format
@ -166,15 +170,16 @@ int main(int argc, const char * argv[]){
hci_dump_init(hci_dump_impl); hci_dump_init(hci_dump_impl);
printf("Packet Log: %s\n", pklg_path); printf("Packet Log: %s\n", pklg_path);
// handle CTRL-c // setup stdin to handle CTRL-c
signal(SIGINT, sigint_handler); btstack_stdin_windows_init();
btstack_stdin_window_register_ctrl_c_callback(&trigger_shutdown);
// setup USB Transport // setup USB Transport
transport = hci_transport_usb_instance(); transport = hci_transport_usb_instance();
btstack_chipset_intel_download_firmware(hci_transport_usb_instance(), &intel_firmware_done); btstack_chipset_intel_download_firmware(hci_transport_usb_instance(), &intel_firmware_done);
// go // go
btstack_run_loop_execute(); btstack_run_loop_execute();
return 0; return 0;
} }

View File

@ -35,7 +35,7 @@
* *
*/ */
#define __BTSTACK_FILE__ "main.c" #define BTSTACK_FILE__ "main.c"
// ***************************************************************************** // *****************************************************************************
// //
@ -51,13 +51,15 @@
#include "btstack_config.h" #include "btstack_config.h"
#include "ble/le_device_db_tlv.h"
#include "btstack_debug.h" #include "btstack_debug.h"
#include "btstack_event.h" #include "btstack_event.h"
#include "btstack_memory.h" #include "btstack_memory.h"
#include "btstack_run_loop.h" #include "btstack_run_loop.h"
#include "btstack_run_loop_windows.h" #include "btstack_run_loop_windows.h"
#include "btstack_stdin.h"
#include "btstack_stdin_windows.h"
#include "btstack_tlv_posix.h" #include "btstack_tlv_posix.h"
#include "ble/le_device_db_tlv.h"
#include "classic/btstack_link_key_db_tlv.h" #include "classic/btstack_link_key_db_tlv.h"
#include "hal_led.h" #include "hal_led.h"
#include "hci.h" #include "hci.h"
@ -65,7 +67,6 @@
#include "hci_dump_posix_fs.h" #include "hci_dump_posix_fs.h"
#include "hci_transport.h" #include "hci_transport.h"
#include "hci_transport_usb.h" #include "hci_transport_usb.h"
#include "btstack_stdin.h"
int btstack_main(int argc, const char * argv[]); int btstack_main(int argc, const char * argv[]);
@ -77,40 +78,45 @@ static char tlv_db_path[100];
static const btstack_tlv_t * tlv_impl; static const btstack_tlv_t * tlv_impl;
static btstack_tlv_posix_t tlv_context; static btstack_tlv_posix_t tlv_context;
static bd_addr_t local_addr; static bd_addr_t local_addr;
static bool shutdown_triggered;
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
if (packet_type != HCI_EVENT_PACKET) return; if (packet_type != HCI_EVENT_PACKET) return;
if (hci_event_packet_get_type(packet) != BTSTACK_EVENT_STATE) return; if (hci_event_packet_get_type(packet) != BTSTACK_EVENT_STATE) return;
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; switch (btstack_event_state_get_state(packet)){
gap_local_bd_addr(local_addr); case HCI_STATE_WORKING:
printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); gap_local_bd_addr(local_addr);
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
strcat(tlv_db_path, bd_addr_to_str(local_addr)); strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); strcat(tlv_db_path, bd_addr_to_str(local_addr));
tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
btstack_tlv_set_instance(tlv_impl, &tlv_context); tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
btstack_tlv_set_instance(tlv_impl, &tlv_context);
#ifdef ENABLE_CLASSIC #ifdef ENABLE_CLASSIC
hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
#endif #endif
#ifdef ENABLE_BLE #ifdef ENABLE_BLE
le_device_db_tlv_configure(tlv_impl, &tlv_context); le_device_db_tlv_configure(tlv_impl, &tlv_context);
#endif #endif
break;
case HCI_STATE_OFF:
btstack_tlv_posix_deinit(&tlv_context);
if (!shutdown_triggered) break;
// reset stdin
btstack_stdin_reset();
log_info("Good bye, see you.\n");
exit(0);
break;
default:
break;
}
} }
static void sigint_handler(int param){ static void trigger_shutdown(void){
UNUSED(param);
printf("CTRL-C - SIGINT received, shutting down..\n"); printf("CTRL-C - SIGINT received, shutting down..\n");
log_info("sigint_handler: shutting down"); log_info("sigint_handler: shutting down");
shutdown_triggered = true;
// reset anyway
btstack_stdin_reset();
// power down
hci_power_control(HCI_POWER_OFF); hci_power_control(HCI_POWER_OFF);
hci_close();
log_info("Good bye, see you.\n");
exit(0);
} }
static int led_state = 0; static int led_state = 0;
@ -145,8 +151,9 @@ int main(int argc, const char * argv[]){
hci_event_callback_registration.callback = &packet_handler; hci_event_callback_registration.callback = &packet_handler;
hci_add_event_handler(&hci_event_callback_registration); hci_add_event_handler(&hci_event_callback_registration);
// handle CTRL-c // setup stdin to handle CTRL-c
signal(SIGINT, sigint_handler); btstack_stdin_windows_init();
btstack_stdin_window_register_ctrl_c_callback(&trigger_shutdown);
// setup app // setup app
btstack_main(argc, argv); btstack_main(argc, argv);