Skip to content

Commit 8e332c4

Browse files
committed
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. Signed-off-by: Riadh Ghaddab <[email protected]>
1 parent 3a35208 commit 8e332c4

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

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

238+
ret = settings_call_set_handler(arg->subtree, rc2, settings_zms_read_fn,
239+
&read_fn_arg, (void *)arg);
240+
if (ret) {
241+
return ret;
242+
}
243+
}
244+
}
245+
/* If subtree is NULL then we must load all found Settings */
210246
ret = zms_read(&cf->cf_zms, ZMS_LL_HEAD_HASH_ID, &settings_element,
211247
sizeof(struct settings_hash_linked_list));
212248
if (ret < 0) {

0 commit comments

Comments
 (0)