1.. _settings_api: 2 3Settings 4######## 5 6The settings subsystem gives modules a way to store persistent per-device 7configuration and runtime state. A variety of storage implementations are 8provided behind a common API using FCB, NVS, ZMS or a file system. These 9different implementations give the application developer flexibility to select 10an appropriate storage medium, and even change it later as needs change. This 11subsystem is used by various Zephyr components and can be used simultaneously by 12user applications. 13 14Settings items are stored as key-value pair strings. By convention, 15the keys can be organized by the package and subtree defining the key, 16for example the key ``id/serial`` would define the ``serial`` configuration 17element for the package ``id``. 18 19Convenience routines are provided for converting a key value to 20and from a string type. 21 22For an example of the settings subsystem refer to :zephyr:code-sample:`settings` sample. 23 24.. note:: 25 26 As of Zephyr release 4.1 the recommended backends for non-filesystem 27 storage are :ref:`NVS <nvs_api>` and :ref:`ZMS <zms_api>`. 28 29Handlers 30******** 31 32Settings handlers for subtree implement a set of handler functions. 33These are registered using a call to :c:func:`settings_register()` for 34dynamic handlers or defined using a call to :c:macro:`SETTINGS_STATIC_HANDLER_DEFINE()` 35for static handlers. 36 37**h_get** 38 This gets called when asking for a settings element value by its name using 39 :c:func:`settings_runtime_get()` from the runtime backend. 40 41**h_set** 42 This gets called when the value is loaded from persistent storage with 43 :c:func:`settings_load()`, or when using :c:func:`settings_runtime_set()` from the 44 runtime backend. 45 46**h_commit** 47 This gets called after the settings have been loaded in full. 48 Sometimes you don't want an individual setting value to take 49 effect right away, for example if there are multiple settings 50 which are interdependent. 51 52**h_export** 53 This gets called to write all current settings. This happens 54 when :c:func:`settings_save()` tries to save the settings or transfer to any 55 user-implemented back-end. 56 57Settings handlers also have a commit priority ``cprio`` that allows to prioritize 58the ``h_commit`` calls. This can be advantageous when e.g. a subsystem initializes 59a service that other ``h_commit`` calls depend on. 60 61Settings handlers ``h_commit`` routines are by default initialized with ``cprio = 0``, 62initializing a settings handler with a different priority is done using a call to 63:c:func:`settings_register_with_cprio()` for dynamic handlers or using a call to 64:c:macro:`SETTINGS_STATIC_HANDLER_DEFINE_WITH_CPRIO()` for static handlers. The 65specified ``cprio`` value is an integer where lower values mean higher priority. 66 67Backends 68******** 69 70Backends are meant to load and save data to/from setting handlers, and 71implement a set of handler functions. These are registered using a call to 72:c:func:`settings_src_register()` for backends that can load data, and/or 73:c:func:`settings_dst_register()` for backends that can save data. The current 74implementation allows for multiple source backends but only a single destination 75backend. 76 77**csi_load** 78 This gets called when loading values from persistent storage using 79 :c:func:`settings_load()`. 80 81**csi_save** 82 This gets called when saving a single setting to persistent storage using 83 :c:func:`settings_save_one()`. 84 85**csi_save_start** 86 This gets called when starting a save of all current settings using 87 :c:func:`settings_save()` or :c:func:`settings_save_subtree()`. 88 89**csi_save_end** 90 This gets called after having saved of all current settings using 91 :c:func:`settings_save()` or :c:func:`settings_save_subtree()`. 92 93Zephyr Storage Backends 94*********************** 95 96Zephyr offers the following storage backends: 97 98* Flash Circular Buffer (:kconfig:option:`CONFIG_SETTINGS_FCB`). 99* A file in the filesystem (:kconfig:option:`CONFIG_SETTINGS_FILE`). 100* Non-Volatile Storage (:kconfig:option:`CONFIG_SETTINGS_NVS`). 101* Zephyr Memory Storage (:kconfig:option:`CONFIG_SETTINGS_ZMS`). 102 103You can declare multiple sources for settings; settings from 104all of these are restored when :c:func:`settings_load()` is called. 105 106There can be only one target for writing settings; this is where 107data is stored when you call :c:func:`settings_save()`, or :c:func:`settings_save_one()`. 108 109FCB read target is registered using :c:func:`settings_fcb_src()`, and write target 110using :c:func:`settings_fcb_dst()`. As a side-effect, :c:func:`settings_fcb_src()` 111initializes the FCB area, so it must be called before calling 112:c:func:`settings_fcb_dst()`. File read target is registered using 113:c:func:`settings_file_src()`, and write target by using :c:func:`settings_file_dst()`. 114 115Non-volatile storage read target is registered using 116:c:func:`settings_nvs_src()`, and write target by using 117:c:func:`settings_nvs_dst()`. 118 119Zephyr Memory Storage (ZMS) read target is registered using :c:func:`settings_zms_src()`, 120and write target is registered using :c:func:`settings_zms_dst()`. 121 122ZMS backend has the particularity of using hash functions to hash the settings 123key before storing it to the persistent storage. This implementation implies 124that some collisions between key's hashes could occur if a big number of 125different keys are stored. This number depends on the selected hash function. 126 127ZMS backend can handle :math:`2^n` maximum collisions where n is defined by 128(:kconfig:option:`SETTINGS_ZMS_MAX_COLLISIONS_BITS`). 129 130 131Storage Location 132**************** 133 134The FCB, non-volatile storage (NVS) and ZMS backends look for a fixed 135partition with label "storage" by default. A different partition can be 136selected by setting the ``zephyr,settings-partition`` property of the 137chosen node in the devicetree. 138 139The file path used by the file backend to store settings is selected via the 140option :kconfig:option:`CONFIG_SETTINGS_FILE_PATH`. 141 142Loading data from persistent storage 143************************************ 144 145A call to :c:func:`settings_load()` uses an ``h_set`` implementation 146to load settings data from storage to volatile memory. 147After all data is loaded, the ``h_commit`` handler is issued, 148signalling the application that the settings were successfully 149retrieved. 150 151Technically FCB and file backends may store some history of the entities. 152This means that the newest data entity is stored after any 153older existing data entities. 154Starting with Zephyr 2.1, the back-end must filter out all old entities and 155call the callback with only the newest entity. 156 157Storing data to persistent storage 158********************************** 159 160A call to :c:func:`settings_save_one()` uses a backend implementation to store 161settings data to the storage medium. A call to :c:func:`settings_save()` uses an 162``h_export`` implementation to store different data in one operation using 163:c:func:`settings_save_one()`. 164A key needs to be covered by a ``h_export`` only if it is supposed to be stored 165by :c:func:`settings_save()` call. 166 167For both FCB and file back-end only storage requests with data which 168changes most actual key's value are stored, therefore there is no need to check 169whether a value changed by the application. Such a storage mechanism implies 170that storage can contain multiple value assignments for a key , while only the 171last is the current value for the key. 172 173Garbage collection 174================== 175When storage becomes full (FCB) or consumes too much space (file), 176the backend removes non-recent key-value pairs records and unnecessary 177key-delete records. 178 179Secure domain settings 180********************** 181Currently settings doesn't provide scheme of being secure, and non-secure 182configuration storage simultaneously for the same instance. 183It is recommended that secure domain uses its own settings instance and it might 184provide data for non-secure domain using dedicated interface if needed 185(case dependent). 186 187Example: Device Configuration 188***************************** 189 190This is a simple example, where the settings handler only implements ``h_set`` 191and ``h_export``. ``h_set`` is called when the value is restored from storage 192(or when set initially), and ``h_export`` is used to write the value to 193storage thanks to ``storage_func()``. The user can also implement some other 194export functionality, for example, writing to the shell console). 195 196.. code-block:: c 197 198 #define DEFAULT_FOO_VAL_VALUE 1 199 200 static int8 foo_val = DEFAULT_FOO_VAL_VALUE; 201 202 static int foo_settings_set(const char *name, size_t len, 203 settings_read_cb read_cb, void *cb_arg) 204 { 205 const char *next; 206 int rc; 207 208 if (settings_name_steq(name, "bar", &next) && !next) { 209 if (len != sizeof(foo_val)) { 210 return -EINVAL; 211 } 212 213 rc = read_cb(cb_arg, &foo_val, sizeof(foo_val)); 214 if (rc >= 0) { 215 /* key-value pair was properly read. 216 * rc contains value length. 217 */ 218 return 0; 219 } 220 /* read-out error */ 221 return rc; 222 } 223 224 return -ENOENT; 225 } 226 227 static int foo_settings_export(int (*storage_func)(const char *name, 228 const void *value, 229 size_t val_len)) 230 { 231 return storage_func("foo/bar", &foo_val, sizeof(foo_val)); 232 } 233 234 struct settings_handler my_conf = { 235 .name = "foo", 236 .h_set = foo_settings_set, 237 .h_export = foo_settings_export 238 }; 239 240Example: Persist Runtime State 241****************************** 242 243This is a simple example showing how to persist runtime state. In this example, 244only ``h_set`` is defined, which is used when restoring value from 245persistent storage. 246 247In this example, the ``main`` function increments ``foo_val``, and then 248persists the latest number. When the system restarts, the application calls 249:c:func:`settings_load()` while initializing, and ``foo_val`` will continue counting 250up from where it was before restart. 251 252.. code-block:: c 253 254 #include <zephyr/kernel.h> 255 #include <zephyr/sys/reboot.h> 256 #include <zephyr/settings/settings.h> 257 #include <zephyr/sys/printk.h> 258 #include <inttypes.h> 259 260 #define DEFAULT_FOO_VAL_VALUE 0 261 262 static uint8_t foo_val = DEFAULT_FOO_VAL_VALUE; 263 264 static int foo_settings_set(const char *name, size_t len, 265 settings_read_cb read_cb, void *cb_arg) 266 { 267 const char *next; 268 int rc; 269 270 if (settings_name_steq(name, "bar", &next) && !next) { 271 if (len != sizeof(foo_val)) { 272 return -EINVAL; 273 } 274 275 rc = read_cb(cb_arg, &foo_val, sizeof(foo_val)); 276 if (rc >= 0) { 277 return 0; 278 } 279 280 return rc; 281 } 282 283 284 return -ENOENT; 285 } 286 287 struct settings_handler my_conf = { 288 .name = "foo", 289 .h_set = foo_settings_set 290 }; 291 292 int main(void) 293 { 294 settings_subsys_init(); 295 settings_register(&my_conf); 296 settings_load(); 297 298 foo_val++; 299 settings_save_one("foo/bar", &foo_val, sizeof(foo_val)); 300 301 printk("foo: %d\n", foo_val); 302 303 k_msleep(1000); 304 sys_reboot(SYS_REBOOT_COLD); 305 } 306 307Example: Custom Backend Implementation 308************************************** 309 310This is a simple example showing how to register a simple custom backend 311handler (:kconfig:option:`CONFIG_SETTINGS_CUSTOM`). 312 313.. code-block:: c 314 315 static int settings_custom_load(struct settings_store *cs, 316 const struct settings_load_arg *arg) 317 { 318 //... 319 } 320 321 static int settings_custom_save(struct settings_store *cs, const char *name, 322 const char *value, size_t val_len) 323 { 324 //... 325 } 326 327 /* custom backend interface */ 328 static struct settings_store_itf settings_custom_itf = { 329 .csi_load = settings_custom_load, 330 .csi_save = settings_custom_save, 331 }; 332 333 /* custom backend node */ 334 static struct settings_store settings_custom_store = { 335 .cs_itf = &settings_custom_itf 336 }; 337 338 int settings_backend_init(void) 339 { 340 /* register custom backend */ 341 settings_dst_register(&settings_custom_store); 342 settings_src_register(&settings_custom_store); 343 return 0; 344 } 345 346API Reference 347************* 348 349The Settings subsystem APIs are provided by :zephyr_file:`include/zephyr/settings/settings.h`. 350 351API for general settings usage 352============================== 353.. doxygengroup:: settings 354 355API for key-name processing 356=========================== 357.. doxygengroup:: settings_name_proc 358 359API for runtime settings manipulation 360===================================== 361.. doxygengroup:: settings_rt 362 363API of backend interface 364======================== 365.. doxygengroup:: settings_backend 366