aboutsummaryrefslogtreecommitdiffhomepage
path: root/app
diff options
context:
space:
mode:
authorJoel Spadin <[email protected]>2023-11-26 12:27:12 -0600
committerPete Johanson <[email protected]>2024-03-18 09:48:19 -0700
commit610a806c848ba53a454bbc66631fa3f3faaef19f (patch)
treeaabf5fc29daae2a97aec1394b6f9d2b1cf3d931c /app
parent2adaa00d1092e41ce11dfae46ce7d2a5bad97ead (diff)
downloadzmk-610a806c848ba53a454bbc66631fa3f3faaef19f.tar.gz
zmk-610a806c848ba53a454bbc66631fa3f3faaef19f.zip
feat: Add function to erase all settings
Added a zmk_settings_erase() function to clear all saved settings. This does not go through Zephyr's settings subsystem, but instead directly clears the data from the setting storage backend, so a reboot is needed for it to take effect.
Diffstat (limited to 'app')
-rw-r--r--app/CMakeLists.txt1
-rw-r--r--app/include/zmk/settings.h15
-rw-r--r--app/src/settings/CMakeLists.txt7
-rw-r--r--app/src/settings/reset_settings_fcb.c15
-rw-r--r--app/src/settings/reset_settings_file.c23
-rw-r--r--app/src/settings/reset_settings_none.c12
-rw-r--r--app/src/settings/reset_settings_nvs.c39
7 files changed, 112 insertions, 0 deletions
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index 6ef0031102..b12d04742e 100644
--- a/app/CMakeLists.txt
+++ b/app/CMakeLists.txt
@@ -99,5 +99,6 @@ target_sources_ifdef(CONFIG_ZMK_LOW_PRIORITY_WORK_QUEUE app PRIVATE src/workqueu
target_sources(app PRIVATE src/main.c)
add_subdirectory(src/display/)
+add_subdirectory_ifdef(CONFIG_SETTINGS src/settings/)
zephyr_cc_option(-Wfatal-errors)
diff --git a/app/include/zmk/settings.h b/app/include/zmk/settings.h
new file mode 100644
index 0000000000..5567d61b42
--- /dev/null
+++ b/app/include/zmk/settings.h
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2023 The ZMK Contributors
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#pragma once
+
+/**
+ * Erases all saved settings.
+ *
+ * @note This does not automatically update any code using Zephyr's settings
+ * subsystem. This should typically be followed by a call to sys_reboot().
+ */
+int zmk_settings_erase(void);
diff --git a/app/src/settings/CMakeLists.txt b/app/src/settings/CMakeLists.txt
new file mode 100644
index 0000000000..63bcd7f952
--- /dev/null
+++ b/app/src/settings/CMakeLists.txt
@@ -0,0 +1,7 @@
+# Copyright (c) 2023 The ZMK Contributors
+# SPDX-License-Identifier: MIT
+
+target_sources_ifdef(CONFIG_SETTINGS_NONE app PRIVATE reset_settings_none.c)
+target_sources_ifdef(CONFIG_SETTINGS_FCB app PRIVATE reset_settings_fcb.c)
+target_sources_ifdef(CONFIG_SETTINGS_FILE app PRIVATE reset_settings_file.c)
+target_sources_ifdef(CONFIG_SETTINGS_NVS app PRIVATE reset_settings_nvs.c)
diff --git a/app/src/settings/reset_settings_fcb.c b/app/src/settings/reset_settings_fcb.c
new file mode 100644
index 0000000000..1d555d341e
--- /dev/null
+++ b/app/src/settings/reset_settings_fcb.c
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2023 The ZMK Contributors
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#include <zmk/settings.h>
+
+#include <zephyr/logging/log.h>
+LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
+
+int zmk_settings_erase(void) {
+ LOG_ERR("Settings reset is not implemented for deprecated FCB backend.");
+ return -ENOSYS;
+}
diff --git a/app/src/settings/reset_settings_file.c b/app/src/settings/reset_settings_file.c
new file mode 100644
index 0000000000..1d142868d3
--- /dev/null
+++ b/app/src/settings/reset_settings_file.c
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2023 The ZMK Contributors
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#include <zephyr/fs/fs.h>
+
+#include <zmk/settings.h>
+
+#include <zephyr/logging/log.h>
+LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
+
+int zmk_settings_erase(void) {
+ LOG_INF("Erasing settings file");
+
+ int rc = fs_unlink(CONFIG_SETTINGS_FS_FILE);
+ if (rc) {
+ LOG_ERR("Failed to unlink '%s': %d", CONFIG_SETTINGS_FS_FILE, rc);
+ }
+
+ return rc;
+}
diff --git a/app/src/settings/reset_settings_none.c b/app/src/settings/reset_settings_none.c
new file mode 100644
index 0000000000..c5e0ae0419
--- /dev/null
+++ b/app/src/settings/reset_settings_none.c
@@ -0,0 +1,12 @@
+/*
+ * Copyright (c) 2023 The ZMK Contributors
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#include <zmk/settings.h>
+
+int zmk_settings_erase(void) {
+ // No storage backend; nothing to do
+ return 0;
+}
diff --git a/app/src/settings/reset_settings_nvs.c b/app/src/settings/reset_settings_nvs.c
new file mode 100644
index 0000000000..65157b3d15
--- /dev/null
+++ b/app/src/settings/reset_settings_nvs.c
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2023 The ZMK Contributors
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#include <zephyr/device.h>
+#include <zephyr/devicetree.h>
+#include <zephyr/storage/flash_map.h>
+
+#include <zephyr/logging/log.h>
+LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
+
+// SETTINGS_PARTITION must match settings_nvs.c
+#if DT_HAS_CHOSEN(zephyr_settings_partition)
+#define SETTINGS_PARTITION DT_FIXED_PARTITION_ID(DT_CHOSEN(zephyr_settings_partition))
+#else
+#define SETTINGS_PARTITION FIXED_PARTITION_ID(storage_partition)
+#endif
+
+int zmk_settings_erase(void) {
+ LOG_INF("Erasing settings flash partition");
+
+ const struct flash_area *fa;
+ int rc = flash_area_open(SETTINGS_PARTITION, &fa);
+ if (rc) {
+ LOG_ERR("Failed to open settings flash: %d", rc);
+ return rc;
+ }
+
+ rc = flash_area_erase(fa, 0, fa->fa_size);
+ if (rc) {
+ LOG_ERR("Failed to erase settings flash: %d", rc);
+ }
+
+ flash_area_close(fa);
+
+ return rc;
+}