mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-04-07 16:20:19 +00:00
qt: rework shutdown on ctrl-c
This commit is contained in:
parent
d5b02eade8
commit
00f9da3aee
@ -126,7 +126,7 @@ ELSE()
|
||||
message("Building for POSIX using libusb")
|
||||
|
||||
set(SOURCES_HCI_USB ${BTSTACK_ROOT}/platform/libusb/hci_transport_h2_libusb.c)
|
||||
set(SOURCES_STDIN ${BTSTACK_ROOT}/platform/posix/btstack_stdin_posix.c)
|
||||
set(SOURCES_STDIN ${BTSTACK_ROOT}/platform/posix/btstack_stdin_posix.c ${BTSTACK_ROOT}/platform/posix/btstack_signal.c)
|
||||
|
||||
list(APPEND SOURCES_POSIX ${BTSTACK_ROOT}/platform/posix/btstack_network_posix.c)
|
||||
list(APPEND SOURCES_POSIX ${BTSTACK_ROOT}/platform/posix/btstack_block_posix.c)
|
||||
|
@ -78,6 +78,13 @@
|
||||
#include "btstack_chipset_csr.h"
|
||||
#include "btstack_chipset_cc256x.h"
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
#include "btstack_stdin_windows.h"
|
||||
#else
|
||||
#include <signal.h>
|
||||
#include "btstack_signal.h"
|
||||
#endif
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
#define TLV_DB_PATH_PREFIX "btstack"
|
||||
#else
|
||||
@ -91,6 +98,9 @@ static btstack_tlv_posix_t tlv_context;
|
||||
static bd_addr_t local_addr;
|
||||
static int is_bcm;
|
||||
|
||||
// shutdown
|
||||
static bool shutdown_triggered;
|
||||
|
||||
extern "C" int btstack_main(int argc, const char * argv[]);
|
||||
|
||||
static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc };
|
||||
@ -168,26 +178,39 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
|
||||
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) return;
|
||||
gap_local_bd_addr(local_addr);
|
||||
if (using_static_address){
|
||||
memcpy(local_addr, static_address, 6);
|
||||
}
|
||||
printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
|
||||
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
|
||||
switch (btstack_event_state_get_state(packet)){
|
||||
case HCI_STATE_WORKING:
|
||||
gap_local_bd_addr(local_addr);
|
||||
if (using_static_address) {
|
||||
memcpy(local_addr, static_address, 6);
|
||||
}
|
||||
printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
|
||||
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
|
||||
#ifndef Q_OS_WIN
|
||||
// bd_addr_to_str use ":" which is not allowed in windows file names
|
||||
strcat(tlv_db_path, bd_addr_to_str(local_addr));
|
||||
// bd_addr_to_str use ":" which is not allowed in windows file names
|
||||
strcat(tlv_db_path, bd_addr_to_str(local_addr));
|
||||
#endif
|
||||
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);
|
||||
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;
|
||||
}
|
||||
break;
|
||||
case HCI_EVENT_COMMAND_COMPLETE:
|
||||
if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
|
||||
@ -208,20 +231,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;
|
||||
@ -273,8 +287,13 @@ int main(int argc, char * argv[]){
|
||||
hci_event_callback_registration.callback = &packet_handler;
|
||||
hci_add_event_handler(&hci_event_callback_registration);
|
||||
|
||||
// handle CTRL-c
|
||||
signal(SIGINT, sigint_handler);
|
||||
// register callback for CTRL-c
|
||||
#ifdef Q_OS_WIN
|
||||
btstack_stdin_windows_init();
|
||||
btstack_stdin_window_register_ctrl_c_callback(&trigger_shutdown);
|
||||
#else
|
||||
btstack_signal_register_callback(SIGINT, &trigger_shutdown);
|
||||
#endif
|
||||
|
||||
// setup app
|
||||
btstack_main(argc, (const char **) argv);
|
||||
|
@ -126,7 +126,7 @@ ELSE()
|
||||
link_libraries(${LIBUSB_LIBRARIES})
|
||||
|
||||
set(SOURCES_HCI_USB ${BTSTACK_ROOT}/platform/libusb/hci_transport_h2_libusb.c)
|
||||
set(SOURCES_STDIN ${BTSTACK_ROOT}/platform/posix/btstack_stdin_posix.c)
|
||||
set(SOURCES_STDIN ${BTSTACK_ROOT}/platform/posix/btstack_stdin_posix.c ${BTSTACK_ROOT}/platform/posix/btstack_signal.c)
|
||||
|
||||
list(APPEND SOURCES_POSIX ${BTSTACK_ROOT}/platform/posix/btstack_network_posix.c)
|
||||
ENDIF()
|
||||
|
@ -72,6 +72,13 @@
|
||||
#include "btstack_tlv_posix.h"
|
||||
#include "btstack_uart_block.h"
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
#include "btstack_stdin_windows.h"
|
||||
#else
|
||||
#include <signal.h>
|
||||
#include "btstack_signal.h"
|
||||
#endif
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
#define TLV_DB_PATH_PREFIX "btstack"
|
||||
#else
|
||||
@ -83,6 +90,8 @@ 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;
|
||||
// shutdown
|
||||
static bool shutdown_triggered;
|
||||
|
||||
extern "C" int btstack_main(int argc, const char * argv[]);
|
||||
|
||||
@ -111,28 +120,41 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
|
||||
UNUSED(channel);
|
||||
UNUSED(size);
|
||||
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:
|
||||
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
|
||||
gap_local_bd_addr(local_addr);
|
||||
if (using_static_address){
|
||||
memcpy(local_addr, static_address, 6);
|
||||
}
|
||||
printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
|
||||
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
|
||||
switch (btstack_event_state_get_state(packet)){
|
||||
case HCI_STATE_WORKING:
|
||||
gap_local_bd_addr(local_addr);
|
||||
if (using_static_address) {
|
||||
memcpy(local_addr, static_address, 6);
|
||||
}
|
||||
printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
|
||||
strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
|
||||
#ifndef Q_OS_WIN
|
||||
// bd_addr_to_str use ":" which is not allowed in windows file names
|
||||
strcat(tlv_db_path, bd_addr_to_str(local_addr));
|
||||
// bd_addr_to_str use ":" which is not allowed in windows file names
|
||||
strcat(tlv_db_path, bd_addr_to_str(local_addr));
|
||||
#endif
|
||||
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);
|
||||
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;
|
||||
}
|
||||
break;
|
||||
case HCI_EVENT_COMMAND_COMPLETE:
|
||||
if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){
|
||||
@ -149,20 +171,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;
|
||||
@ -259,8 +272,13 @@ int main(int argc, char * argv[]){
|
||||
hci_event_callback_registration.callback = &packet_handler;
|
||||
hci_add_event_handler(&hci_event_callback_registration);
|
||||
|
||||
// handle CTRL-c
|
||||
signal(SIGINT, sigint_handler);
|
||||
// register callback for CTRL-c
|
||||
#ifdef Q_OS_WIN
|
||||
btstack_stdin_windows_init();
|
||||
btstack_stdin_window_register_ctrl_c_callback(&trigger_shutdown);
|
||||
#else
|
||||
btstack_signal_register_callback(SIGINT, &trigger_shutdown);
|
||||
#endif
|
||||
|
||||
// setup app
|
||||
btstack_main(argc, (const char **) argv);
|
||||
|
Loading…
x
Reference in New Issue
Block a user