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.
This commit is contained in:
Joel Spadin 2023-04-29 23:24:11 -05:00
parent 4b57588884
commit c072f75563
4 changed files with 41 additions and 14 deletions

View file

@ -31,7 +31,7 @@ bool zmk_ble_active_profile_is_open();
bool zmk_ble_active_profile_is_connected(); bool zmk_ble_active_profile_is_connected();
char *zmk_ble_active_profile_name(); 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) #if IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
void zmk_ble_set_peripheral_addr(bt_addr_le_t *addr); void zmk_ble_set_peripheral_addr(bt_addr_le_t *addr);

View file

@ -14,3 +14,11 @@
* @param type A ZMK_RESET_* value indicating how to reboot. * @param type A ZMK_RESET_* value indicating how to reboot.
*/ */
FUNC_NORETURN void zmk_reset(int type); 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);

View file

@ -38,6 +38,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#if IS_ENABLED(CONFIG_ZMK_BLE_PASSKEY_ENTRY) #if IS_ENABLED(CONFIG_ZMK_BLE_PASSKEY_ENTRY)
#include <zmk/events/keycode_state_changed.h> #include <zmk/events/keycode_state_changed.h>
#include "ble.h"
#define PASSKEY_DIGITS 6 #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; } 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) #if IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
void zmk_ble_set_peripheral_addr(bt_addr_le_t *addr) { 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 #endif
#if IS_ENABLED(CONFIG_ZMK_BLE_CLEAR_BONDS_ON_START) #if IS_ENABLED(CONFIG_ZMK_BLE_CLEAR_BONDS_ON_START)
LOG_WRN("Clearing all existing BLE bond information from the keyboard"); zmk_ble_unpair_all();
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);
}
}
#endif #endif
bt_conn_cb_register(&conn_callbacks); bt_conn_cb_register(&conn_callbacks);

View file

@ -6,7 +6,11 @@
#include <zephyr/sys/reboot.h> #include <zephyr/sys/reboot.h>
#include <zephyr/logging/log.h> #include <zephyr/logging/log.h>
#include <zephyr/sys/util.h>
#if IS_ENABLED(CONFIG_ZMK_BLE)
#include <zmk/ble.h>
#endif
#include <zmk/reset.h> #include <zmk/reset.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
@ -35,3 +39,10 @@ FUNC_NORETURN void zmk_reset(int type) {
break; break;
} }
} }
void zmk_reset_settings(void) {
#if IS_ENABLED(CONFIG_ZMK_BLE)
zmk_ble_unpair_all();
#endif
// TODO: clear settings for all subsystems.
}