From f11fd9a990fedbf11b7c70e38b9da44019506e13 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Thu, 1 Jul 2021 22:12:30 +0200 Subject: [PATCH] posix: use bstack_signal to trigger shutdown for ctrl-c --- port/libusb-intel/Makefile | 7 ++- port/libusb-intel/main.c | 74 +++++++++++++------------ port/libusb/Makefile | 5 +- port/libusb/main.c | 44 ++++++++------- port/posix-h4-atwilc3000/Makefile | 4 ++ port/posix-h4-atwilc3000/main.c | 64 ++++++++++++---------- port/posix-h4-da14581/Makefile | 4 ++ port/posix-h4-da14581/main.c | 73 +++++++++++++------------ port/posix-h4-da14585/Makefile | 4 ++ port/posix-h4-da14585/main.c | 67 ++++++++++++----------- port/posix-h4-zephyr/Makefile | 4 ++ port/posix-h4-zephyr/main.c | 61 +++++++++++---------- port/posix-h4/Makefile | 4 ++ port/posix-h4/main.c | 83 ++++++++++++++-------------- port/posix-h5/Makefile | 4 ++ port/posix-h5/main.c | 89 ++++++++++++++++--------------- port/raspi/Makefile | 6 ++- port/raspi/main.c | 86 +++++++++++++++-------------- 18 files changed, 377 insertions(+), 306 deletions(-) diff --git a/port/libusb-intel/Makefile b/port/libusb-intel/Makefile index 01e88f753..0adf7117b 100644 --- a/port/libusb-intel/Makefile +++ b/port/libusb-intel/Makefile @@ -3,8 +3,8 @@ BTSTACK_ROOT ?= ../.. CORE += main.c btstack_stdin_posix.c btstack_tlv_posix.c hci_dump_posix_fs.c -COMMON += hci_transport_h2_libusb.c btstack_run_loop_posix.c le_device_db_tlv.c btstack_link_key_db_tlv.c wav_util.c btstack_network_posix.c -COMMON += btstack_audio_portaudio.c btstack_chipset_intel_firmware.c rijndael.c +COMMON += hci_transport_h2_libusb.c btstack_run_loop_posix.c le_device_db_tlv.c btstack_link_key_db_tlv.c wav_util.c btstack_network_posix.c +COMMON += btstack_audio_portaudio.c btstack_chipset_intel_firmware.c rijndael.c btstack_signal.c include ${BTSTACK_ROOT}/example/Makefile.inc include ${BTSTACK_ROOT}/chipset/intel/Makefile.inc @@ -34,6 +34,9 @@ VPATH += ${BTSTACK_ROOT}/chipset/intel CFLAGS += $(shell pkg-config libusb-1.0 --cflags) LDFLAGS += $(shell pkg-config libusb-1.0 --libs) +# add pthread for ctrl-c signal handler +LDFLAGS += -lpthread + EXAMPLES = ${EXAMPLES_GENERAL} ${EXAMPLES_CLASSIC_ONLY} ${EXAMPLES_LE_ONLY} ${EXAMPLES_DUAL_MODE} EXAMPLES += pan_lwip_http_server diff --git a/port/libusb-intel/main.c b/port/libusb-intel/main.c index 006dfea11..8603f9cea 100644 --- a/port/libusb-intel/main.c +++ b/port/libusb-intel/main.c @@ -35,7 +35,7 @@ * */ -#define __BTSTACK_FILE__ "main.c" +#define BTSTACK_FILE__ "main.c" // ***************************************************************************** // @@ -51,23 +51,24 @@ #include "btstack_config.h" +#include "ble/le_device_db_tlv.h" +#include "btstack_audio.h" +#include "btstack_chipset_intel_firmware.h" #include "btstack_debug.h" #include "btstack_event.h" -#include "ble/le_device_db_tlv.h" -#include "classic/btstack_link_key_db_tlv.h" #include "btstack_memory.h" #include "btstack_run_loop.h" #include "btstack_run_loop_posix.h" +#include "btstack_signal.h" +#include "btstack_stdin.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_usb.h" -#include "btstack_stdin.h" -#include "btstack_audio.h" -#include "btstack_tlv_posix.h" -#include "btstack_chipset_intel_firmware.h" #define TLV_DB_PATH_PREFIX "/tmp/btstack_" #define TLV_DB_PATH_POSTFIX ".tlv" @@ -80,6 +81,8 @@ static int main_argc; static const char ** main_argv; static const hci_transport_t * transport; static int intel_firmware_loaded; +// shutdown +static bool shutdown_triggered; int btstack_main(int argc, const char * argv[]); @@ -91,43 +94,44 @@ 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); - 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; + } 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(); - } - - log_info("Good bye, see you.\n"); - exit(0); + shutdown_triggered = true; + hci_power_control(HCI_POWER_OFF); } static int led_state = 0; @@ -208,8 +212,8 @@ int main(int argc, const char * argv[]){ transport = hci_transport_usb_instance(); btstack_chipset_intel_download_firmware(transport, &intel_firmware_done); - // handle CTRL-c - signal(SIGINT, sigint_handler); + // register callback for CTRL-c + btstack_signal_register_callback(SIGINT, &trigger_shutdown); // go btstack_run_loop_execute(); diff --git a/port/libusb/Makefile b/port/libusb/Makefile index b7dfe6d3d..96b9a0094 100644 --- a/port/libusb/Makefile +++ b/port/libusb/Makefile @@ -4,7 +4,7 @@ BTSTACK_ROOT ?= ../.. CORE += main.c btstack_stdin_posix.c btstack_tlv_posix.c hci_dump_posix_fs.c COMMON += hci_transport_h2_libusb.c btstack_run_loop_posix.c le_device_db_tlv.c btstack_link_key_db_tlv.c wav_util.c btstack_network_posix.c -COMMON += btstack_audio_portaudio.c btstack_chipset_zephyr.c rijndael.c +COMMON += btstack_audio_portaudio.c btstack_chipset_zephyr.c rijndael.c btstack_signal.c include ${BTSTACK_ROOT}/example/Makefile.inc @@ -34,6 +34,9 @@ VPATH += ${BTSTACK_ROOT}/chipset/zephyr CFLAGS += $(shell pkg-config libusb-1.0 --cflags) LDFLAGS += $(shell pkg-config libusb-1.0 --libs) +# add pthread for ctrl-c signal handler +LDFLAGS += -lpthread + EXAMPLES = ${EXAMPLES_GENERAL} ${EXAMPLES_CLASSIC_ONLY} ${EXAMPLES_LE_ONLY} ${EXAMPLES_DUAL_MODE} EXAMPLES += pan_lwip_http_server EXAMPLES += csr_set_bd_addr diff --git a/port/libusb/main.c b/port/libusb/main.c index 0c93fb7a1..1dd46dd76 100644 --- a/port/libusb/main.c +++ b/port/libusb/main.c @@ -35,7 +35,7 @@ * */ -#define __BTSTACK_FILE__ "main.c" +#define BTSTACK_FILE__ "main.c" // ***************************************************************************** // @@ -47,28 +47,28 @@ #include #include #include -#include #include "btstack_config.h" +#include "ble/le_device_db_tlv.h" #include "bluetooth_company_id.h" +#include "btstack_audio.h" +#include "btstack_chipset_zephyr.h" #include "btstack_debug.h" #include "btstack_event.h" -#include "ble/le_device_db_tlv.h" -#include "classic/btstack_link_key_db_tlv.h" #include "btstack_memory.h" #include "btstack_run_loop.h" #include "btstack_run_loop_posix.h" +#include "btstack_signal.h" +#include "btstack_stdin.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_usb.h" -#include "btstack_stdin.h" -#include "btstack_audio.h" -#include "btstack_tlv_posix.h" -#include "btstack_chipset_zephyr.h" #define TLV_DB_PATH_PREFIX "/tmp/btstack_" #define TLV_DB_PATH_POSTFIX ".tlv" @@ -86,6 +86,9 @@ static int using_static_address; static btstack_packet_callback_registration_t hci_event_callback_registration; +// shutdown +static bool shutdown_triggered; + static void local_version_information_handler(uint8_t * packet){ printf("Local version information:\n"); uint16_t hci_version = packet[6]; @@ -138,11 +141,15 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack 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; } - if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; break; case HCI_EVENT_COMMAND_COMPLETE: if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){ @@ -159,20 +166,11 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack } } -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 int led_state = 0; @@ -239,8 +237,8 @@ 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); + // register callback for CTRL-c + btstack_signal_register_callback(SIGINT, &trigger_shutdown); // setup app btstack_main(argc, argv); diff --git a/port/posix-h4-atwilc3000/Makefile b/port/posix-h4-atwilc3000/Makefile index d65781dcf..65db5ca95 100644 --- a/port/posix-h4-atwilc3000/Makefile +++ b/port/posix-h4-atwilc3000/Makefile @@ -12,6 +12,7 @@ CORE += \ main.c \ wav_util.c \ btstack_stdin_posix.c \ + btstack_signal.c \ # examples CLASSIC = @@ -31,6 +32,9 @@ CFLAGS += -g -Wall -Werror \ VPATH += ${BTSTACK_ROOT}/platform/posix VPATH += ${BTSTACK_ROOT}/chipset/atwilc3000 +# add pthread for ctrl-c signal handler +LDFLAGS += -lpthread + EXAMPLES = ${EXAMPLES_GENERAL} ${EXAMPLES_LE_ONLY} # use pkg-config for portaudio diff --git a/port/posix-h4-atwilc3000/main.c b/port/posix-h4-atwilc3000/main.c index e58b6f3e9..b6f722885 100644 --- a/port/posix-h4-atwilc3000/main.c +++ b/port/posix-h4-atwilc3000/main.c @@ -51,21 +51,22 @@ #include "btstack_config.h" +#include "ble/le_device_db_tlv.h" #include "btstack_debug.h" #include "btstack_event.h" -#include "ble/le_device_db_tlv.h" -#include "classic/btstack_link_key_db_tlv.h" #include "btstack_memory.h" #include "btstack_run_loop.h" #include "btstack_run_loop_posix.h" +#include "btstack_signal.h" +#include "btstack_stdin.h" +#include "btstack_tlv_posix.h" #include "btstack_uart.h" +#include "classic/btstack_link_key_db_tlv.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_tlv_posix.h" #include "btstack_chipset_atwilc3000.h" // #include "wilc3000_bt_firmware.h" @@ -81,6 +82,9 @@ 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; +// shutdown +static bd_addr_t local_addr; +static bool shutdown_triggered; int btstack_main(int argc, const char * argv[]); @@ -95,42 +99,44 @@ 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); + 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_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; 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 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; @@ -157,8 +163,8 @@ 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); + // register callback for CTRL-c + btstack_signal_register_callback(SIGINT, &trigger_shutdown); // setup app btstack_main(main_argc, main_argv); diff --git a/port/posix-h4-da14581/Makefile b/port/posix-h4-da14581/Makefile index 3f35a1624..39e0443bd 100644 --- a/port/posix-h4-da14581/Makefile +++ b/port/posix-h4-da14581/Makefile @@ -13,6 +13,7 @@ CORE += \ main.c \ wav_util.c \ btstack_stdin_posix.c \ + btstack_signal.c \ # examples CLASSIC = @@ -24,6 +25,9 @@ CFLAGS += -g -Wall -Werror \ -I$(BTSTACK_ROOT)/platform/embedded \ -I${BTSTACK_ROOT}/3rd-party/tinydir +# add pthread for ctrl-c signal handler +LDFLAGS += -lpthread + VPATH += ${BTSTACK_ROOT}/platform/posix VPATH += ${BTSTACK_ROOT}/chipset/da14581 diff --git a/port/posix-h4-da14581/main.c b/port/posix-h4-da14581/main.c index 1121bbb54..8bc288e24 100644 --- a/port/posix-h4-da14581/main.c +++ b/port/posix-h4-da14581/main.c @@ -35,7 +35,7 @@ * */ -#define __BTSTACK_FILE__ "main.c" +#define BTSTACK_FILE__ "main.c" // ***************************************************************************** // @@ -51,23 +51,23 @@ #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_posix.h" +#include "btstack_signal.h" +#include "btstack_stdin.h" +#include "btstack_tlv_posix.h" #include "btstack_uart.h" -#include "ble/le_device_db_tlv.h" #include "hci.h" +#include "hci_581_active_uart.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 "btstack_chipset_da14581.h" -#include "hci_581_active_uart.h" static int main_argc; static const char ** main_argv; @@ -79,6 +79,10 @@ 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; + +static btstack_packet_callback_registration_t hci_event_callback_registration; int btstack_main(int argc, const char * argv[]); @@ -90,43 +94,42 @@ static hci_transport_config_uart_t transport_config = { NULL, }; -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); - break; + 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); + le_device_db_tlv_configure(tlv_impl, &tlv_context); + 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; + } 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 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; @@ -152,8 +155,8 @@ 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); + // register callback for CTRL-c + btstack_signal_register_callback(SIGINT, &trigger_shutdown); // setup app btstack_main(main_argc, main_argv); diff --git a/port/posix-h4-da14585/Makefile b/port/posix-h4-da14585/Makefile index dd849f534..ab2f7729d 100644 --- a/port/posix-h4-da14585/Makefile +++ b/port/posix-h4-da14585/Makefile @@ -11,6 +11,7 @@ CORE += \ le_device_db_tlv.c \ main.c \ btstack_stdin_posix.c \ + btstack_signal.c \ hci_585.c \ # hci_581_active_uart.c \ @@ -24,6 +25,9 @@ CFLAGS += -g -Wall -Werror \ -I$(BTSTACK_ROOT)/platform/embedded \ -I${BTSTACK_ROOT}/3rd-party/tinydir +# add pthread for ctrl-c signal handler +LDFLAGS += -lpthread + VPATH += ${BTSTACK_ROOT}/platform/posix VPATH += ${BTSTACK_ROOT}/chipset/da14581 diff --git a/port/posix-h4-da14585/main.c b/port/posix-h4-da14585/main.c index 9dbaa4928..2f81207c0 100644 --- a/port/posix-h4-da14585/main.c +++ b/port/posix-h4-da14585/main.c @@ -35,7 +35,7 @@ * */ -#define __BTSTACK_FILE__ "main.c" +#define BTSTACK_FILE__ "main.c" // ***************************************************************************** // @@ -51,23 +51,23 @@ #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_posix.h" +#include "btstack_signal.h" +#include "btstack_stdin.h" +#include "btstack_tlv_posix.h" #include "btstack_uart.h" -#include "ble/le_device_db_tlv.h" #include "hci.h" +#include "hci_581_active_uart.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 "btstack_chipset_da14581.h" -#include "hci_581_active_uart.h" static int main_argc; static const char ** main_argv; @@ -79,6 +79,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[]); @@ -93,40 +95,43 @@ 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); + le_device_db_tlv_configure(tlv_impl, &tlv_context); + 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); - 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 int led_state = 0; @@ -152,8 +157,8 @@ 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); + // register callback for CTRL-c + btstack_signal_register_callback(SIGINT, &trigger_shutdown); // setup app btstack_main(main_argc, main_argv); diff --git a/port/posix-h4-zephyr/Makefile b/port/posix-h4-zephyr/Makefile index 9a3fefc50..8ede9188f 100644 --- a/port/posix-h4-zephyr/Makefile +++ b/port/posix-h4-zephyr/Makefile @@ -10,6 +10,7 @@ CORE += \ le_device_db_tlv.c \ main.c \ btstack_stdin_posix.c \ + btstack_signal.c \ btstack_chipset_zephyr.c \ # examples @@ -21,6 +22,9 @@ CFLAGS += -g -Wall -Werror \ -I$(BTSTACK_ROOT)/platform/posix \ -I$(BTSTACK_ROOT)/chipset/zephyr \ -I${BTSTACK_ROOT}/3rd-party/tinydir + +# add pthread for ctrl-c signal handler +LDFLAGS += -lpthread VPATH += ${BTSTACK_ROOT}/platform/posix VPATH += ${BTSTACK_ROOT}/platform/embedded diff --git a/port/posix-h4-zephyr/main.c b/port/posix-h4-zephyr/main.c index fcaa62690..fc0e64a4b 100644 --- a/port/posix-h4-zephyr/main.c +++ b/port/posix-h4-zephyr/main.c @@ -51,22 +51,23 @@ #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_posix.h" +#include "btstack_signal.h" +#include "btstack_stdin.h" +#include "btstack_tlv_posix.h" #include "btstack_uart.h" -#include "bluetooth_company_id.h" -#include "ble/le_device_db_tlv.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 "btstack_tlv_posix.h" int btstack_main(int argc, const char * argv[]); @@ -75,6 +76,7 @@ int btstack_main(int argc, const char * argv[]); 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 hci_transport_config_uart_t config = { HCI_TRANSPORT_CONFIG_UART, @@ -93,15 +95,28 @@ 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) break; - printf("BTstack up and running as %s\n", bd_addr_to_str(static_address)); - // setup TLV - 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); - le_device_db_tlv_configure(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)); + // setup TLV + 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); + le_device_db_tlv_configure(tlv_impl, &tlv_context); + 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){ @@ -114,20 +129,12 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack } } -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 int led_state = 0; @@ -170,8 +177,8 @@ 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); + // register callback for CTRL-c + btstack_signal_register_callback(SIGINT, &trigger_shutdown); // setup app btstack_main(argc, argv); diff --git a/port/posix-h4/Makefile b/port/posix-h4/Makefile index 2df963d9c..3df8575cf 100644 --- a/port/posix-h4/Makefile +++ b/port/posix-h4/Makefile @@ -21,6 +21,7 @@ CORE += \ rijndael.c \ main.c \ btstack_stdin_posix.c \ + btstack_signal.c \ wav_util.c \ 0000000_META_hci_patches_v7.c \ # bluetooth_init_cc2564_2.14.c \ @@ -60,6 +61,9 @@ VPATH += ${BTSTACK_ROOT}/chipset/em9301 VPATH += ${BTSTACK_ROOT}/chipset/stlc2500d VPATH += ${BTSTACK_ROOT}/chipset/tc3566x +# add pthread for ctrl-c signal handler +LDFLAGS += -lpthread + EXAMPLES = ${EXAMPLES_GENERAL} ${EXAMPLES_CLASSIC_ONLY} ${EXAMPLES_LE_ONLY} ${EXAMPLES_DUAL_MODE} EXAMPLES += pan_lwip_http_server diff --git a/port/posix-h4/main.c b/port/posix-h4/main.c index 1b18ffe49..da125a30e 100644 --- a/port/posix-h4/main.c +++ b/port/posix-h4/main.c @@ -35,7 +35,7 @@ * */ -#define __BTSTACK_FILE__ "main.c" +#define BTSTACK_FILE__ "main.c" // ***************************************************************************** // @@ -51,30 +51,30 @@ #include "btstack_config.h" +#include "ble/le_device_db_tlv.h" +#include "bluetooth_company_id.h" #include "btstack_audio.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 "ble/le_device_db_tlv.h" -#include "classic/btstack_link_key_db_tlv.h" #include "btstack_memory.h" #include "btstack_run_loop.h" #include "btstack_run_loop_posix.h" +#include "btstack_signal.h" +#include "btstack_stdin.h" +#include "btstack_tlv_posix.h" #include "btstack_uart.h" -#include "bluetooth_company_id.h" +#include "classic/btstack_link_key_db_tlv.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_tlv_posix.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" #define TLV_DB_PATH_PREFIX "/tmp/btstack_" @@ -82,8 +82,11 @@ 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 int is_bcm; +// shutdown +static bool shutdown_triggered; int btstack_main(int argc, const char * argv[]); static void local_version_information_handler(uint8_t * packet); @@ -99,25 +102,36 @@ static hci_transport_config_uart_t 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); + 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; + } break; case HCI_EVENT_COMMAND_COMPLETE: if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){ @@ -138,20 +152,11 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack } } -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 int led_state = 0; @@ -282,8 +287,8 @@ 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); + // register callback for CTRL-c + btstack_signal_register_callback(SIGINT, &trigger_shutdown); // setup app btstack_main(argc, argv); diff --git a/port/posix-h5/Makefile b/port/posix-h5/Makefile index fff7ee2c1..4ee1b5945 100644 --- a/port/posix-h5/Makefile +++ b/port/posix-h5/Makefile @@ -19,6 +19,7 @@ CORE += \ main.c \ wav_util.c \ btstack_stdin_posix.c \ + btstack_signal.c \ # TI-WL183x requires TIInit_11.8.32.c @@ -38,6 +39,9 @@ CFLAGS += -g -Wall -Werror \ -I$(BTSTACK_ROOT)/chipset/tc3566x \ -I${BTSTACK_ROOT}/3rd-party/tinydir +# add pthread for ctrl-c signal handler +LDFLAGS += -lpthread + VPATH += ${BTSTACK_ROOT}/platform/posix VPATH += ${BTSTACK_ROOT}/platform/embedded diff --git a/port/posix-h5/main.c b/port/posix-h5/main.c index f6c15efb6..f30e846cc 100644 --- a/port/posix-h5/main.c +++ b/port/posix-h5/main.c @@ -35,7 +35,7 @@ * */ -#define __BTSTACK_FILE__ "main.c" +#define BTSTACK_FILE__ "main.c" // ***************************************************************************** // @@ -51,28 +51,28 @@ #include "btstack_config.h" +#include "ble/le_device_db_tlv.h" +#include "bluetooth_company_id.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 "ble/le_device_db_tlv.h" -#include "classic/btstack_link_key_db_tlv.h" #include "btstack_memory.h" #include "btstack_run_loop.h" #include "btstack_run_loop_posix.h" -#include "bluetooth_company_id.h" +#include "btstack_signal.h" +#include "btstack_stdin.h" +#include "btstack_tlv_posix.h" +#include "btstack_uart.h" +#include "classic/btstack_link_key_db_tlv.h" #include "hci.h" #include "hci_dump.h" #include "hci_dump_posix_fs.h" #include "hci_transport.h" #include "hci_transport_h5.h" -#include "btstack_stdin.h" -#include "btstack_tlv_posix.h" -#include "btstack_uart.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[]); @@ -81,6 +81,8 @@ int btstack_main(int argc, const char * argv[]); 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 hci_transport_config_uart_t config = { HCI_TRANSPORT_CONFIG_UART, @@ -94,21 +96,6 @@ static hci_transport_config_uart_t config = { static btstack_packet_callback_registration_t hci_event_callback_registration; static void local_version_information_handler(uint8_t * packet); -static void sigint_handler(int param){ - UNUSED(param); - - printf("CTRL-C - SIGINT received, shutting down..\n"); - log_info("sigint_handler: shutting down"); - - // reset anyway - btstack_stdin_reset(); - - // power down - hci_power_control(HCI_POWER_OFF); - hci_close(); - log_info("Good bye, see you.\n"); - exit(0); -} static int led_state = 0; void hal_led_toggle(void){ @@ -163,25 +150,36 @@ static void local_version_information_handler(uint8_t * packet){ } 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); + 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; + } break; case HCI_EVENT_COMMAND_COMPLETE: if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){ @@ -199,6 +197,13 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack } } +static void trigger_shutdown(void){ + printf("CTRL-C - SIGINT received, shutting down..\n"); + log_info("sigint_handler: shutting down"); + shutdown_triggered = true; + hci_power_control(HCI_POWER_OFF); +} + int main(int argc, const char * argv[]){ /// GET STARTED with BTstack /// @@ -242,8 +247,8 @@ 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); + // register callback for CTRL-c + btstack_signal_register_callback(SIGINT, &trigger_shutdown); // setup app btstack_main(argc, argv); diff --git a/port/raspi/Makefile b/port/raspi/Makefile index f8ee64ad3..803b1f07e 100644 --- a/port/raspi/Makefile +++ b/port/raspi/Makefile @@ -17,6 +17,7 @@ CORE += \ main.c \ wav_util.c \ btstack_stdin_posix.c \ + btstack_signal.c \ raspi_get_model.c \ rijndael.c @@ -35,9 +36,12 @@ CFLAGS += -g -Wall -Werror \ -# add 'real time' lib for clock_gettime +# add 'real time' lib for clock_gettime, LDFLAGS += -lrt +# add pthread for ctrl-c signal handler +LDFLAGS += -lpthread + VPATH += ${BTSTACK_ROOT}/3rd-party/rijndael VPATH += ${BTSTACK_ROOT}/platform/posix VPATH += ${BTSTACK_ROOT}/platform/embedded diff --git a/port/raspi/main.c b/port/raspi/main.c index ad1162dfb..ec1f7a423 100644 --- a/port/raspi/main.c +++ b/port/raspi/main.c @@ -35,7 +35,7 @@ * */ -#define __BTSTACK_FILE__ "main.c" +#define BTSTACK_FILE__ "main.c" // ***************************************************************************** // @@ -57,33 +57,32 @@ #include "btstack_config.h" +#include "ble/le_device_db_tlv.h" +#include "bluetooth_company_id.h" +#include "btstack_chipset_bcm.h" +#include "btstack_chipset_bcm_download_firmware.h" +#include "btstack_control_raspi.h" #include "btstack_debug.h" #include "btstack_event.h" -#include "ble/le_device_db_tlv.h" -#include "classic/btstack_link_key_db_tlv.h" #include "btstack_memory.h" #include "btstack_run_loop.h" #include "btstack_run_loop_posix.h" -#include "bluetooth_company_id.h" +#include "btstack_signal.h" +#include "btstack_stdin.h" +#include "btstack_tlv_posix.h" +#include "btstack_uart.h" +#include "btstack_uart_block.h" +#include "btstack_uart_slip_wrapper.h" +#include "classic/btstack_link_key_db_tlv.h" #include "hci.h" #include "hci_dump.h" #include "hci_dump_posix_fs.h" #include "hci_transport.h" #include "hci_transport_h4.h" #include "hci_transport_h5.h" -#include "btstack_stdin.h" -#include "btstack_tlv_posix.h" -#include "btstack_uart.h" -#include "btstack_uart_block.h" -#include "btstack_uart_slip_wrapper.h" - -#include "btstack_chipset_bcm.h" -#include "btstack_chipset_bcm_download_firmware.h" -#include "btstack_control_raspi.h" - - #include "raspi_get_model.h" + int btstack_main(int argc, const char * argv[]); typedef enum { @@ -114,6 +113,9 @@ static btstack_packet_callback_registration_t hci_event_callback_registration; 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; static int raspi_speed_to_baud(speed_t baud) @@ -185,20 +187,11 @@ static void raspi_get_terminal_params( hci_transport_config_uart_t *tc ) } } -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 int led_state = 0; @@ -208,25 +201,36 @@ void hal_led_toggle(void){ } 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); + 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; + } break; case HCI_EVENT_COMMAND_COMPLETE: if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){ @@ -402,8 +406,8 @@ 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); + // register callback for CTRL-c + btstack_signal_register_callback(SIGINT, &trigger_shutdown); main_argc = argc; main_argv = argv;