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 <fs/nvs.h>
12 #include "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 char *flash_dev_name;
41 };
42 
43 /* register nvs to be a source of settings */
44 int settings_nvs_src(struct settings_nvs *cf);
45 
46 /* register nvs to be the destination of settings */
47 int settings_nvs_dst(struct settings_nvs *cf);
48 
49 /* Initialize a nvs backend. */
50 int settings_nvs_backend_init(struct settings_nvs *cf);
51 
52 
53 #ifdef __cplusplus
54 }
55 #endif
56 
57 #endif /* __SETTINGS_NVS_H_ */
58