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 <stdlib.h>
#include <string.h>
#include <signal.h>
#include "btstack_config.h"
#include "ble/le_device_db_tlv.h"
#include "btstack_chipset_da14581.h"
#include "btstack_debug.h"
#include "btstack_event.h"
#include "btstack_memory.h"
#include "btstack_run_loop.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_585.h"
#include "hci_dump.h"
#include "hci_dump_posix_fs.h"
#include "hci_transport.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 const char ** main_argv;
@ -78,6 +78,8 @@ static btstack_uart_config_t uart_config;
static char tlv_db_path[100];
static const btstack_tlv_t * tlv_impl;
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[]);
@ -92,40 +94,47 @@ static hci_transport_config_uart_t transport_config = {
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){
bd_addr_t addr;
if (packet_type != HCI_EVENT_PACKET) return;
switch (hci_event_packet_get_type(packet)){
case BTSTACK_EVENT_STATE:
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
gap_local_bd_addr(addr);
printf("BTstack up and running at %s\n", bd_addr_to_str(addr));
// setup TLV
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
strcat(tlv_db_path, bd_addr_to_str(addr));
strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
btstack_tlv_set_instance(tlv_impl, &tlv_context);
le_device_db_tlv_configure(tlv_impl, &tlv_context);
switch (btstack_event_state_get_state(packet)){
case HCI_STATE_WORKING:
gap_local_bd_addr(local_addr);
printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
strcat(tlv_db_path, bd_addr_to_str(local_addr));
strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
btstack_tlv_set_instance(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;
default:
break;
}
}
static void sigint_handler(int param){
UNUSED(param);
static void trigger_shutdown(void){
printf("CTRL-C - SIGINT received, shutting down..\n");
log_info("sigint_handler: shutting down");
// reset anyway
btstack_stdin_reset();
// power down
shutdown_triggered = true;
hci_power_control(HCI_POWER_OFF);
hci_close();
log_info("Good bye, see you.\n");
exit(0);
}
static int led_state = 0;
@ -151,8 +160,9 @@ static void phase2(int status){
hci_event_callback_registration.callback = &packet_handler;
hci_add_event_handler(&hci_event_callback_registration);
// handle CTRL-c
signal(SIGINT, sigint_handler);
// setup stdin to handle CTRL-c
btstack_stdin_windows_init();
btstack_stdin_window_register_ctrl_c_callback(&trigger_shutdown);
// setup app
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 <stdlib.h>
#include <string.h>
#include <signal.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_event.h"
#include "btstack_memory.h"
#include "btstack_run_loop.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_dump.h"
#include "hci_dump_posix_fs.h"
#include "hci_transport.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[]);
@ -88,27 +88,40 @@ static bd_addr_t static_address;
static char tlv_db_path[100];
static const btstack_tlv_t * tlv_impl;
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){
bd_addr_t addr;
if (packet_type != HCI_EVENT_PACKET) return;
switch (hci_event_packet_get_type(packet)){
case BTSTACK_EVENT_STATE:
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
printf("BTstack up and running as %s\n", bd_addr_to_str(static_address));
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
strcat(tlv_db_path, bd_addr_to_str(static_address));
strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
btstack_tlv_set_instance(tlv_impl, &tlv_context);
switch (btstack_event_state_get_state(packet)){
case HCI_STATE_WORKING:
printf("BTstack up and running as %s\n", bd_addr_to_str(static_address));
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
strcat(tlv_db_path, bd_addr_to_str(static_address));
strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
btstack_tlv_set_instance(tlv_impl, &tlv_context);
#ifdef ENABLE_BLE
le_device_db_tlv_configure(tlv_impl, &tlv_context);
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;
case HCI_EVENT_COMMAND_COMPLETE:
if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0){
reverse_48(&packet[7], static_address);
gap_random_address_set(addr);
gap_random_address_set(static_address);
}
break;
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){
UNUSED(param);
static void trigger_shutdown(void){
printf("CTRL-C - SIGINT received, shutting down..\n");
log_info("sigint_handler: shutting down");
// reset anyway
btstack_stdin_reset();
// power down
shutdown_triggered = true;
hci_power_control(HCI_POWER_OFF);
hci_close();
log_info("Good bye, see you.\n");
exit(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_add_event_handler(&hci_event_callback_registration);
// handle CTRL-c
signal(SIGINT, sigint_handler);
// setup stdin to handle CTRL-c
btstack_stdin_windows_init();
btstack_stdin_window_register_ctrl_c_callback(&trigger_shutdown);
// setup app
btstack_main(argc, argv);

View File

@ -35,50 +35,48 @@
*
*/
#define __BTSTACK_FILE__ "main.c"
#define BTSTACK_FILE__ "main.c"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.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_event.h"
#include "btstack_memory.h"
#include "btstack_run_loop.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_dump.h"
#include "hci_dump_posix_fs.h"
#include "hci_transport.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[]);
static void local_version_information_handler(uint8_t * packet);
static hci_transport_config_uart_t config = {
HCI_TRANSPORT_CONFIG_UART,
115200,
0, // main baudrate
1, // flow control
NULL,
HCI_TRANSPORT_CONFIG_UART,
115200,
0, // main baudrate
1, // flow control
NULL,
};
int is_bcm;
@ -91,50 +89,53 @@ static char tlv_db_path[100];
static const btstack_tlv_t * tlv_impl;
static btstack_tlv_posix_t tlv_context;
static bd_addr_t local_addr;
static bool shutdown_triggered;
void hal_led_toggle(void){
led_state = 1 - led_state;
printf("LED State %u\n", led_state);
}
static void sigint_handler(int param){
UNUSED(param);
printf("CTRL-C = SIGINT received, shutting down..\n");
static void trigger_shutdown(void){
printf("CTRL-C - SIGINT received, shutting down..\n");
log_info("sigint_handler: shutting down");
// reset anyway
btstack_stdin_reset();
// power down
shutdown_triggered = true;
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 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;
switch (hci_event_packet_get_type(packet)){
case BTSTACK_EVENT_STATE:
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
gap_local_bd_addr(addr);
printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
strcat(tlv_db_path, bd_addr_to_str(local_addr));
strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
btstack_tlv_set_instance(tlv_impl, &tlv_context);
switch (btstack_event_state_get_state(packet)){
case HCI_STATE_WORKING:
gap_local_bd_addr(local_addr);
printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
strcat(tlv_db_path, bd_addr_to_str(local_addr));
strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
btstack_tlv_set_instance(tlv_impl, &tlv_context);
#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
#ifdef ENABLE_BLE
le_device_db_tlv_configure(tlv_impl, &tlv_context);
le_device_db_tlv_configure(tlv_impl, &tlv_context);
#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;
case HCI_EVENT_COMMAND_COMPLETE:
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_add_event_handler(&hci_event_callback_registration);
// handle CTRL-c
signal(SIGINT, sigint_handler);
// setup stdin to handle CTRL-c
btstack_stdin_windows_init();
btstack_stdin_window_register_ctrl_c_callback(&trigger_shutdown);
// setup app
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 <stdlib.h>
#include <string.h>
#include <signal.h>
#include "btstack_config.h"
#include "ble/le_device_db_tlv.h"
#include "btstack_chipset_intel_firmware.h"
#include "btstack_debug.h"
#include "btstack_event.h"
#include "btstack_memory.h"
#include "btstack_run_loop.h"
#include "btstack_run_loop_windows.h"
#include "btstack_stdin.h"
#include "btstack_stdin_windows.h"
#include "btstack_tlv_posix.h"
#include "ble/le_device_db_tlv.h"
#include "classic/btstack_link_key_db_tlv.h"
#include "hal_led.h"
#include "hci.h"
#include "hci_dump.h"
#include "hci_dump_posix_fs.h"
#include "hci_transport.h"
#include "hci_transport_h4.h"
#include "btstack_stdin.h"
#include "btstack_chipset_intel_firmware.h"
#include "hci_transport_usb.h"
int btstack_main(int argc, const char * argv[]);
@ -83,41 +83,45 @@ static int main_argc;
static const char ** main_argv;
static const hci_transport_t * transport;
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){
if (packet_type != HCI_EVENT_PACKET) return;
if (hci_event_packet_get_type(packet) != BTSTACK_EVENT_STATE) return;
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
gap_local_bd_addr(local_addr);
printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
strcat(tlv_db_path, bd_addr_to_str(local_addr));
strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
btstack_tlv_set_instance(tlv_impl, &tlv_context);
switch (btstack_event_state_get_state(packet)){
case HCI_STATE_WORKING:
gap_local_bd_addr(local_addr);
printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
strcat(tlv_db_path, bd_addr_to_str(local_addr));
strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
btstack_tlv_set_instance(tlv_impl, &tlv_context);
#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
#ifdef ENABLE_BLE
le_device_db_tlv_configure(tlv_impl, &tlv_context);
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;
}
}
static void sigint_handler(int param){
UNUSED(param);
printf("CTRL-C - SIGINT received, shutting down..\n");
static void trigger_shutdown(void){
printf("CTRL-C - SIGINT received, shutting down..\n");
log_info("sigint_handler: shutting down");
// reset anyway
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);
shutdown_triggered = true;
hci_power_control(HCI_POWER_OFF);
}
static int led_state = 0;
@ -155,8 +159,8 @@ int main(int argc, const char * argv[]){
printf("BTstack/windows-winusb booting up\n");
/// GET STARTED with BTstack ///
btstack_memory_init();
/// GET STARTED with BTstack ///
btstack_memory_init();
btstack_run_loop_init(btstack_run_loop_windows_get_instance());
// 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);
printf("Packet Log: %s\n", pklg_path);
// handle CTRL-c
signal(SIGINT, sigint_handler);
// setup stdin to handle CTRL-c
btstack_stdin_windows_init();
btstack_stdin_window_register_ctrl_c_callback(&trigger_shutdown);
// setup USB Transport
transport = hci_transport_usb_instance();
btstack_chipset_intel_download_firmware(hci_transport_usb_instance(), &intel_firmware_done);
// go
btstack_run_loop_execute();
btstack_run_loop_execute();
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 "ble/le_device_db_tlv.h"
#include "btstack_debug.h"
#include "btstack_event.h"
#include "btstack_memory.h"
#include "btstack_run_loop.h"
#include "btstack_run_loop_windows.h"
#include "btstack_stdin.h"
#include "btstack_stdin_windows.h"
#include "btstack_tlv_posix.h"
#include "ble/le_device_db_tlv.h"
#include "classic/btstack_link_key_db_tlv.h"
#include "hal_led.h"
#include "hci.h"
@ -65,7 +67,6 @@
#include "hci_dump_posix_fs.h"
#include "hci_transport.h"
#include "hci_transport_usb.h"
#include "btstack_stdin.h"
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 btstack_tlv_posix_t tlv_context;
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){
if (packet_type != HCI_EVENT_PACKET) return;
if (hci_event_packet_get_type(packet) != BTSTACK_EVENT_STATE) return;
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
gap_local_bd_addr(local_addr);
printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
strcat(tlv_db_path, bd_addr_to_str(local_addr));
strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
btstack_tlv_set_instance(tlv_impl, &tlv_context);
switch (btstack_event_state_get_state(packet)){
case HCI_STATE_WORKING:
gap_local_bd_addr(local_addr);
printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
strcat(tlv_db_path, bd_addr_to_str(local_addr));
strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
btstack_tlv_set_instance(tlv_impl, &tlv_context);
#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
#ifdef ENABLE_BLE
le_device_db_tlv_configure(tlv_impl, &tlv_context);
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;
}
}
static void sigint_handler(int param){
UNUSED(param);
static void trigger_shutdown(void){
printf("CTRL-C - SIGINT received, shutting down..\n");
log_info("sigint_handler: shutting down");
// reset anyway
btstack_stdin_reset();
// power down
shutdown_triggered = true;
hci_power_control(HCI_POWER_OFF);
hci_close();
log_info("Good bye, see you.\n");
exit(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_add_event_handler(&hci_event_callback_registration);
// handle CTRL-c
signal(SIGINT, sigint_handler);
// setup stdin to handle CTRL-c
btstack_stdin_windows_init();
btstack_stdin_window_register_ctrl_c_callback(&trigger_shutdown);
// setup app
btstack_main(argc, argv);