1 /*
2  * Copyright (c) 2017 Nordic Semiconductor ASA
3  * Copyright (c) 2015 Runtime Inc
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <fs/fcb.h>
9 #include "fcb_priv.h"
10 
11 /*
12  * Call 'cb' for every element in flash circular buffer. If sector is specified,
13  * only elements with that flash_sector are reported.
14  */
15 int
fcb_walk(struct fcb * fcb,struct flash_sector * sector,fcb_walk_cb cb,void * cb_arg)16 fcb_walk(struct fcb *fcb, struct flash_sector *sector, fcb_walk_cb cb,
17 	 void *cb_arg)
18 {
19 	struct fcb_entry_ctx entry_ctx;
20 	int rc;
21 
22 	entry_ctx.loc.fe_sector = sector;
23 	entry_ctx.loc.fe_elem_off = 0U;
24 
25 	rc = k_mutex_lock(&fcb->f_mtx, K_FOREVER);
26 	if (rc < 0) {
27 		return -EINVAL;
28 	}
29 	while ((rc = fcb_getnext_nolock(fcb, &entry_ctx.loc)) !=
30 	       -ENOTSUP) {
31 		k_mutex_unlock(&fcb->f_mtx);
32 		if (sector && entry_ctx.loc.fe_sector != sector) {
33 			return 0;
34 		}
35 
36 		entry_ctx.fap = fcb->fap;
37 
38 		rc = cb(&entry_ctx, cb_arg);
39 		if (rc) {
40 			return rc;
41 		}
42 		rc = k_mutex_lock(&fcb->f_mtx, K_FOREVER);
43 		if (rc < 0) {
44 			return -EINVAL;
45 		}
46 	}
47 	k_mutex_unlock(&fcb->f_mtx);
48 	return 0;
49 }
50