# Copyright (c) 2018 Nordic Semiconductor ASA
# Copyright (c) 2025 Analog Devices, Inc.
# SPDX-License-Identifier: Apache-2.0

menuconfig SETTINGS
	bool "Settings"
	help
	  The settings subsystem allows its users to serialize and
	  deserialize state in memory into and from non-volatile memory.
	  It supports several back-ends to store and load serialized data from
	  and it can do so atomically for all involved modules.

if SETTINGS

module = SETTINGS
module-str = settings
source "subsys/logging/Kconfig.template.log_config"

config SETTINGS_RUNTIME
	bool "runtime storage back-end"
	help
	  Enables runtime storage back-end.

config SETTINGS_DYNAMIC_HANDLERS
	bool "dynamic settings handlers"
	default y
	help
	  Enables the use of dynamic settings handlers

config SETTINGS_SAVE_SINGLE_SUBTREE_WITHOUT_MODIFICATION
	bool "Save single or subtree (without modification) function"
	help
	  Includes the `settings_save_subtree_or_single_without_modification()` function which
	  allows saving an item that might be a subtree or a single setting, without changing the
	  value. To save a single setting, the setting must support reading and writing of it
	  using the `h_get()` function pointer and must support reading it into a large buffer
	  size.

config SETTINGS_SAVE_SINGLE_SUBTREE_WITHOUT_MODIFICATION_VALUE_SIZE
	int "Save single or subtree (without modification) maximum value size"
	default 65
	depends on SETTINGS_SAVE_SINGLE_SUBTREE_WITHOUT_MODIFICATION
	help
	  The maximum size of a single setting that can be saved using the
	  `settings_save_subtree_or_single_without_modification()` function - note that this will
	  use stack memory.

# Hidden option to enable encoding length into settings entry
config SETTINGS_ENCODE_LEN
	bool

DT_CHOSEN_ZEPHYR_SETTINGS_PARTITION := zephyr,settings-partition
DT_ZEPHYR_RETENTION := zephyr,retention

config SETTINGS_SUPPORTED_RETENTION
	bool
	default y if RETENTION && $(dt_chosen_has_compat,$(DT_CHOSEN_ZEPHYR_SETTINGS_PARTITION),$(DT_ZEPHYR_RETENTION))

choice SETTINGS_BACKEND
	prompt "Storage back-end"
	default SETTINGS_ZMS if ZMS
	default SETTINGS_NVS if NVS
	default SETTINGS_FCB if FCB
	default SETTINGS_FILE if FILE_SYSTEM
	default SETTINGS_RETENTION if SETTINGS_SUPPORTED_RETENTION
	default SETTINGS_NONE
	help
	  Storage back-end to be used by the settings subsystem.

config SETTINGS_ZMS
	bool "ZMS (Zephyr Memory Storage)"
	depends on ZMS
	depends on FLASH_MAP
	select SYS_HASH_FUNC32
	help
	  Use ZMS as settings storage backend.

if SETTINGS_ZMS

config SETTINGS_ZMS_LL_CACHE
	bool "ZMS linked list lookup cache"
	help
	  Enable ZMS lookup cache for linked list, used to reduce the
	  Settings load time by having most linked list elements already
	  in cache.

config SETTINGS_ZMS_LL_CACHE_SIZE
	int "ZMS linked list lookup cache size"
	default 128
	range 1 $(UINT32_MAX)
	depends on SETTINGS_ZMS_LL_CACHE
	help
	  Number of entries in Settings ZMS linked list cache.

endif # SETTINGS_ZMS

config SETTINGS_FCB
	bool "FCB"
	depends on FCB
	help
	  Use FCB as a settings storage back-end.

config SETTINGS_FILE
	bool "File"
	depends on FILE_SYSTEM
	select SETTINGS_ENCODE_LEN
	help
	  Use a file (on mounted file system) as a settings storage back-end.

config SETTINGS_NVS
	bool "NVS non-volatile storage support"
	depends on NVS
	depends on FLASH_MAP
	help
	  Enables NVS storage support

if SETTINGS_NVS

config SETTINGS_NVS_NAME_CACHE
	bool "NVS name lookup cache"
	help
	  Enable NVS name lookup cache, used to reduce the Settings name
	  lookup time.

config SETTINGS_NVS_NAME_CACHE_SIZE
	int "NVS name lookup cache size"
	default 128
	range 1 $(UINT16_MAX)
	depends on SETTINGS_NVS_NAME_CACHE
	help
	  Number of entries in Settings NVS name cache.

endif # SETTINGS_NVS

config SETTINGS_RETENTION
	bool "Retention storage support"
	depends on SETTINGS_SUPPORTED_RETENTION
	help
	  Enables retention storage support (bulk load/save supported only).

config SETTINGS_CUSTOM
	bool "CUSTOM"
	help
	  Use a custom settings storage back-end.

config SETTINGS_TFM_PSA
	bool "TF-M PSA"
	depends on BUILD_WITH_TFM
	select EXPERIMENTAL
	help
	  Enables PSA Settings backend. Intended for use with boards
	  using TF-M which cannot make use of persistent storage otherwise.
	  This backend uses the PSA ITS or PS service to store settings data.
	  Note: This settings backend compacts settings data into as few secure storage nodes as possible.
	  After each save, the entire settings array is written to the secure storage. To avoid excessive
	  latency on saving, settings are saved after a delay using a kernel work item.
	  Note: With this backend, all settings are kept in RAM at all times. The RAM consumption
	  is controlled by the SETTINGS_TFM_PSA_NUM_ENTRIES Kconfig option.

config SETTINGS_NONE
	bool "NONE"
	help
	  No storage back-end.
endchoice

config SETTINGS_FCB_NUM_AREAS
	int "Number of flash areas used by the settings subsystem"
	default 8
	depends on SETTINGS_FCB
	help
	  Number of areas to allocate in the settings FCB. A smaller number is
	  used if the flash hardware cannot support this value.

config SETTINGS_FCB_MAGIC
	hex "FCB magic for the settings subsystem"
	default 0xc0ffeeee
	depends on SETTINGS_FCB
	help
	  Magic 32-bit word for to identify valid settings area

config SETTINGS_FILE_PATH
	string "Default settings file"
	default "/settings/run"
	depends on SETTINGS_FILE
	help
	  Full path to the default settings file.

config SETTINGS_FILE_MAX_LINES
	int "Compression threshold"
	default 32
	depends on SETTINGS_FILE
	help
	  Limit how many items stored in a file before compressing

config SETTINGS_NVS_SECTOR_SIZE_MULT
	int "Sector size of the NVS settings area"
	default 1
	depends on SETTINGS_NVS
	help
	  The sector size to use for the NVS settings area as a multiple of
	  FLASH_ERASE_BLOCK_SIZE.

config SETTINGS_NVS_SECTOR_COUNT
	int "Sector count of the NVS settings area"
	default 8
	depends on SETTINGS_NVS
	help
	  Number of sectors used for the NVS settings area

config SETTINGS_ZMS_SECTOR_SIZE_MULT
	int "Sector size of the ZMS settings area"
	default 1
	depends on SETTINGS_ZMS
	help
	  The sector size to use for the ZMS settings area as a multiple of
	  FLASH_ERASE_BLOCK_SIZE.

config SETTINGS_ZMS_CUSTOM_SECTOR_COUNT
	bool "Customize the sector count of the ZMS settings partition"
	depends on SETTINGS_ZMS
	help
	  The number of sectors used by default is the maximum value that can
	  fit in the settings storage partition.
	  Enabling this config allows to customize the number of used sectors.

config SETTINGS_ZMS_SECTOR_COUNT
	int "Sector count of the ZMS settings area"
	default 8
	depends on SETTINGS_ZMS && SETTINGS_ZMS_CUSTOM_SECTOR_COUNT
	help
	  Number of sectors used for the ZMS settings area

config SETTINGS_ZMS_MAX_COLLISIONS_BITS
	int "number of bits reserved to handle collisions between hash numbers"
	default 4
	depends on SETTINGS_ZMS
	help
	  The maximum number of hash collisions needs to be well sized depending
	  on the data that is going to be stored in ZMS and its hash values

config SETTINGS_ZMS_NO_LL_DELETE
	bool "Disable deletion of Linked list hashes"
	help
	  For some applications, the Settings delete operation is too long for
	  ZMS because of the linked list update.
	  As a tradeoff for performance the linked list is not updated. As a
	  result, some nodes will be unused and will occupy some space in the
	  storage.
	  These nodes will be used again when the same Settings element that has
	  been deleted is created again.

config SETTINGS_ZMS_LOAD_SUBTREE_PATH
	bool "Load only subtree path if provided"
	help
	  Loads first the key defined by the subtree path.
	  If the callback handler returns a zero value it will
	  continue to look for all the keys under that subtree path.
	  If the callback handler returns a non negative value, it
	  returns immeditaley.

config SETTINGS_SHELL
	bool "Settings shell"
	depends on SHELL
	help
	  Enable shell commands for listing and reading the settings. Note that
	  reading the settings requires quite a big stack buffer, so the stack
	  size of the shell thread may need to be increased to accommodate this
	  feature.

if SETTINGS_TFM_PSA

choice SETTINGS_TFM_PSA_BACKEND
	prompt "TF-M PSA Settings backend"
	default SETTINGS_TFM_PSA_BACKEND_ITS
	help
	  PSA storage option to be used by the PSA settings backend.

config SETTINGS_TFM_PSA_BACKEND_PS
	bool "PSA Protected Storage (PS)"
	depends on TFM_PARTITION_PROTECTED_STORAGE
	help
	  Use PSA Protected Storage as settings storage backend.

config SETTINGS_TFM_PSA_BACKEND_ITS
	bool "PSA Internal Trusted Storage (ITS)"
	depends on TFM_PARTITION_INTERNAL_TRUSTED_STORAGE
	help
	  Use PSA Internal Trusted Storage as settings storage backend.

endchoice # SETTINGS_TFM_PSA_BACKEND

config SETTINGS_TFM_PSA_NUM_ENTRIES
	int "Maximum number of settings entries"
	default 10
	help
	  Configures the maximum number of settings that can be stored simultaneously.
	  Note: This value determines the size of a statically-allocated buffer which holds
	  all the settings entries in RAM at all times.

config SETTINGS_TFM_PSA_LAZY_PERSIST_DELAY_MS
	int "Milliseconds delay before persisting settings"
	default 500
	help
	  PSA Secure Storage APIs may block for a long period of time when writing to flash, which may be
	  unacceptable for time-sensitive events.
	  Data is always persisted to the secure storage using k_work_delayable, instead of happening in
	  the same context as settings_save. This option sets the delay with which the
	  work item is scheduled. The delay is useful in cases where a write may
	  block a time-sensitive event, for example Bluetooth pairing, which requires a
	  sequence of settings writes.

endif # SETTINGS_TFM_PSA

endif # SETTINGS
