From c072f75563ab855588a14d5de043d7ef7973924d Mon Sep 17 00:00:00 2001 From: Joel Spadin Date: Sat, 29 Apr 2023 23:24:11 -0500 Subject: [PATCH] refactor: Add a settings reset function For now, this just clears BLE bonds, but it can be updated in the future to handle clearing all settings. --- app/include/zmk/ble.h | 2 +- app/include/zmk/reset.h | 8 ++++++++ app/src/ble.c | 34 +++++++++++++++++++++------------- app/src/reset.c | 11 +++++++++++ 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/app/include/zmk/ble.h b/app/include/zmk/ble.h index 4380a33a..5f51a092 100644 --- a/app/include/zmk/ble.h +++ b/app/include/zmk/ble.h @@ -31,7 +31,7 @@ bool zmk_ble_active_profile_is_open(); bool zmk_ble_active_profile_is_connected(); char *zmk_ble_active_profile_name(); -int zmk_ble_unpair_all(); +void zmk_ble_unpair_all(void); #if IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) void zmk_ble_set_peripheral_addr(bt_addr_le_t *addr); diff --git a/app/include/zmk/reset.h b/app/include/zmk/reset.h index d23d1d8e..fad6db6a 100644 --- a/app/include/zmk/reset.h +++ b/app/include/zmk/reset.h @@ -14,3 +14,11 @@ * @param type A ZMK_RESET_* value indicating how to reboot. */ FUNC_NORETURN void zmk_reset(int type); + +/** + * Clear all persistent settings. + * + * This should typically be followed by a call to zmk_reset() to ensure that + * all subsystems are properly reset. + */ +void zmk_reset_settings(void); diff --git a/app/src/ble.c b/app/src/ble.c index a7037d0c..5a1d81c5 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -38,6 +38,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #if IS_ENABLED(CONFIG_ZMK_BLE_PASSKEY_ENTRY) #include +#include "ble.h" #define PASSKEY_DIGITS 6 @@ -278,6 +279,25 @@ bt_addr_le_t *zmk_ble_active_profile_addr() { return &profiles[active_profile].p char *zmk_ble_active_profile_name() { return profiles[active_profile].name; } +void zmk_ble_unpair_all(void) { + LOG_WRN("Clearing all existing BLE bond information from the keyboard"); + + int err = bt_unpair(BT_ID_DEFAULT, NULL); + if (err) { + LOG_ERR("Failed to unpair default identity: %d", err); + } + + for (int i = 0; i < ZMK_BLE_PROFILE_COUNT; i++) { + char setting_name[15]; + sprintf(setting_name, "ble/profiles/%d", i); + + err = settings_delete(setting_name); + if (err) { + LOG_ERR("Failed to delete setting: %d", err); + } + } +} + #if IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) void zmk_ble_set_peripheral_addr(bt_addr_le_t *addr) { @@ -575,19 +595,7 @@ static int zmk_ble_init(const struct device *_arg) { #endif #if IS_ENABLED(CONFIG_ZMK_BLE_CLEAR_BONDS_ON_START) - LOG_WRN("Clearing all existing BLE bond information from the keyboard"); - - bt_unpair(BT_ID_DEFAULT, NULL); - - for (int i = 0; i < ZMK_BLE_PROFILE_COUNT; i++) { - char setting_name[15]; - sprintf(setting_name, "ble/profiles/%d", i); - - err = settings_delete(setting_name); - if (err) { - LOG_ERR("Failed to delete setting: %d", err); - } - } + zmk_ble_unpair_all(); #endif bt_conn_cb_register(&conn_callbacks); diff --git a/app/src/reset.c b/app/src/reset.c index 0470f519..c0fff987 100644 --- a/app/src/reset.c +++ b/app/src/reset.c @@ -6,7 +6,11 @@ #include #include +#include +#if IS_ENABLED(CONFIG_ZMK_BLE) +#include +#endif #include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -35,3 +39,10 @@ FUNC_NORETURN void zmk_reset(int type) { break; } } + +void zmk_reset_settings(void) { +#if IS_ENABLED(CONFIG_ZMK_BLE) + zmk_ble_unpair_all(); +#endif + // TODO: clear settings for all subsystems. +} \ No newline at end of file