1 /*
2  * Copyright (c) 2019 Laczen
3  * Copyright (c) 2019 Nordic Semiconductor ASA
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #ifndef __SETTINGS_NVS_H_
9 #define __SETTINGS_NVS_H_
10 
11 #include <zephyr/fs/nvs.h>
12 #include <zephyr/settings/settings.h>
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /* In the NVS backend, each setting is stored in two NVS entries:
19  *	1. setting's name
20  *	2. setting's value
21  *
22  * The NVS entry ID for the setting's value is determined implicitly based on
23  * the ID of the NVS entry for the setting's name, once that is found. The
24  * difference between name and value ID is constant and equal to
25  * NVS_NAME_ID_OFFSET.
26  *
27  * Setting's name entries start from NVS_NAMECNT_ID + 1. The entry at
28  * NVS_NAMECNT_ID is used to store the largest name ID in use.
29  *
30  * Deleted records will not be found, only the last record will be
31  * read.
32  */
33 #define NVS_NAMECNT_ID 0x8000
34 #define NVS_NAME_ID_OFFSET 0x4000
35 
36 struct settings_nvs {
37 	struct settings_store cf_store;
38 	struct nvs_fs cf_nvs;
39 	uint16_t last_name_id;
40 	const struct device *flash_dev;
41 #if CONFIG_SETTINGS_NVS_NAME_CACHE
42 	struct {
43 		uint16_t name_hash;
44 		uint16_t name_id;
45 	} cache[CONFIG_SETTINGS_NVS_NAME_CACHE_SIZE];
46 
47 	uint16_t cache_next;
48 	uint16_t cache_total;
49 	bool loaded;
50 #endif
51 };
52 
53 /* register nvs to be a source of settings */
54 int settings_nvs_src(struct settings_nvs *cf);
55 
56 /* register nvs to be the destination of settings */
57 int settings_nvs_dst(struct settings_nvs *cf);
58 
59 /* Initialize a nvs backend. */
60 int settings_nvs_backend_init(struct settings_nvs *cf);
61 
62 
63 #ifdef __cplusplus
64 }
65 #endif
66 
67 #endif /* __SETTINGS_NVS_H_ */
68