Skip to content

Commit 6466716

Browse files
committed
[nrf fromlist] settings: zms: load only subtree in the argument
If the subtree argument is not NULL in the settings_load function, load only the setting entry that corresponds to that subtree. If the subtree argument is NULL or it is not found in the storage, load all the existing entries in the storage. Upstream PR #: 87792 Signed-off-by: Riadh Ghaddab <[email protected]> (cherry picked from commit 410076f584a75c4554994be8ccc943766a3cc4de)
1 parent 8ad282a commit 6466716

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

subsys/settings/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,16 @@ config SETTINGS_ZMS_NO_LL_DELETE
201201
These nodes will be used again when the same Settings element that has
202202
been deleted is created again.
203203

204+
config SETTINGS_ZMS_LOAD_SUBTREE_PATH
205+
bool "Load only subtree path if provided"
206+
help
207+
Some applications use the settings_load_subtree to load only one
208+
settings entry instead of a all entries under that path.
209+
Enabling this config will allow the ZMS backend to try to load that
210+
path first.
211+
If the callback handler returns a zero value it will
212+
continue to look for all the keys under that subtree path.
213+
204214
config SETTINGS_SHELL
205215
bool "Settings shell"
206216
depends on SHELL

subsys/settings/include/settings/settings_zms.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ extern "C" {
7474
#define ZMS_NAME_ID_FROM_LL_NODE(x) (x & ~BIT(0))
7575
#define ZMS_UPDATE_COLLISION_NUM(x, y) \
7676
((x & ~ZMS_COLLISIONS_MASK) | ((y << 1) & ZMS_COLLISIONS_MASK))
77-
#define ZMS_COLLISION_NUM(x) ((x & ZMS_COLLISIONS_MASK) >> 1)
77+
#define ZMS_COLLISION_NUM(x) ((x & ZMS_COLLISIONS_MASK) >> 1)
78+
#define ZMS_NAME_ID_FROM_HASH(x) ((x & ZMS_HASH_TOTAL_MASK) | BIT(31))
7879

7980
struct settings_zms {
8081
struct settings_store cf_store;

subsys/settings/src/settings_zms.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,52 @@ static int settings_zms_load(struct settings_store *cs, const struct settings_lo
206206
ssize_t rc1;
207207
ssize_t rc2;
208208
uint32_t ll_hash_id;
209+
#ifdef CONFIG_SETTINGS_ZMS_LOAD_SUBTREE_PATH
210+
uint32_t name_hash;
211+
212+
/* If arg->subtree is not null we must load settings in that subtree */
213+
if (arg->subtree != NULL) {
214+
name_hash = sys_hash32(arg->subtree, strlen(arg->subtree)) & ZMS_HASH_MASK;
215+
for (int i = 0; i <= cf->hash_collision_num; i++) {
216+
name_hash = ZMS_UPDATE_COLLISION_NUM(name_hash, i);
217+
/* Get the name entry from ZMS */
218+
rc1 = zms_read(&cf->cf_zms, ZMS_NAME_ID_FROM_HASH(name_hash), &name,
219+
sizeof(name) - 1);
220+
/* get the length of data and verify that it exists */
221+
rc2 = zms_get_data_length(&cf->cf_zms, ZMS_NAME_ID_FROM_HASH(name_hash) +
222+
ZMS_DATA_ID_OFFSET);
223+
if ((rc1 <= 0) || (rc2 <= 0)) {
224+
/* Name or data doesn't exist */
225+
continue;
226+
}
227+
/* Found a name, this might not include a trailing \0 */
228+
name[rc1] = '\0';
229+
if (strcmp(arg->subtree, name)) {
230+
/* Names are not equal let's continue to the next collision hash
231+
* if it exists.
232+
*/
233+
continue;
234+
}
235+
/* At this steps the names are equal, let's set the handler */
236+
read_fn_arg.fs = &cf->cf_zms;
237+
read_fn_arg.id = ZMS_NAME_ID_FROM_HASH(name_hash) + ZMS_DATA_ID_OFFSET;
209238

239+
ret = settings_call_set_handler(arg->subtree, rc2, settings_zms_read_fn,
240+
&read_fn_arg, (void *)arg);
241+
if (ret) {
242+
return ret;
243+
}
244+
}
245+
}
246+
#endif /* CONFIG_SETTINGS_ZMS_LOAD_SUBTREE_PATH */
210247
ret = zms_read(&cf->cf_zms, ZMS_LL_HEAD_HASH_ID, &settings_element,
211248
sizeof(struct settings_hash_linked_list));
212249
if (ret < 0) {
213250
return ret;
214251
}
215252
ll_hash_id = settings_element.next_hash;
216253

254+
/* If subtree is NULL then we must load all found Settings */
217255
while (ll_hash_id) {
218256

219257
/* In the ZMS backend, each setting item is stored in two ZMS

0 commit comments

Comments
 (0)