diff --git a/include/fcb.h b/include/fcb.h index 0d4249da1b..49d595d177 100644 --- a/include/fcb.h +++ b/include/fcb.h @@ -53,7 +53,6 @@ struct fcb { u8_t f_version; /* Current version number of the data */ u8_t f_sector_cnt; /* Number of elements in sector array */ u8_t f_scratch_cnt; /* How many sectors should be kept empty */ - int f_area_id; struct flash_sector *f_sectors; /* Array of sectors, */ /* must be contiguous */ @@ -80,7 +79,7 @@ struct fcb { #define FCB_ERR_CRC -6 #define FCB_ERR_MAGIC -7 -int fcb_init(struct fcb *fcb); +int fcb_init(int f_area_id, struct fcb *fcb); /* * fcb_append() appends an entry to circular buffer. When writing the diff --git a/subsys/fs/fcb/fcb.c b/subsys/fs/fcb/fcb.c index 5627396e55..97efd6ee77 100644 --- a/subsys/fs/fcb/fcb.c +++ b/subsys/fs/fcb/fcb.c @@ -12,54 +12,34 @@ #include "fcb_priv.h" #include "string.h" -const struct flash_area * -fcb_open_flash(const struct fcb *fcb) -{ - const struct flash_area *fa; - int rc; - - rc = flash_area_open(fcb->f_area_id, &fa); - if (rc != 0) { - return NULL; - } - - return fa; -} - u8_t fcb_get_align(const struct fcb *fcb) { - const struct flash_area *fa; u8_t align; - fa = fcb_open_flash(fcb); - if (fa == NULL) { + if (fcb->fap == NULL) { return 0; } - align = flash_area_align(fa); + align = flash_area_align(fcb->fap); - flash_area_close(fa); return align; } int fcb_flash_read(const struct fcb *fcb, const struct flash_sector *sector, off_t off, void *dst, size_t len) { - const struct flash_area *fa; int rc; if (off + len > sector->fs_size) { return FCB_ERR_ARGS; } - fa = fcb_open_flash(fcb); - if (fa == NULL) { + if (fcb->fap == NULL) { return FCB_ERR_FLASH; } - rc = flash_area_read(fa, sector->fs_off + off, dst, len); - flash_area_close(fa); + rc = flash_area_read(fcb->fap, sector->fs_off + off, dst, len); if (rc != 0) { return FCB_ERR_FLASH; @@ -71,20 +51,17 @@ int fcb_flash_read(const struct fcb *fcb, const struct flash_sector *sector, int fcb_flash_write(const struct fcb *fcb, const struct flash_sector *sector, off_t off, const void *src, size_t len) { - const struct flash_area *fa; int rc; if (off + len > sector->fs_size) { return FCB_ERR_ARGS; } - fa = fcb_open_flash(fcb); - if (fa == NULL) { + if (fcb->fap == NULL) { return FCB_ERR_FLASH; } - rc = flash_area_write(fa, sector->fs_off + off, src, len); - flash_area_close(fa); + rc = flash_area_write(fcb->fap, sector->fs_off + off, src, len); if (rc != 0) { return FCB_ERR_FLASH; @@ -96,16 +73,13 @@ int fcb_flash_write(const struct fcb *fcb, const struct flash_sector *sector, int fcb_erase_sector(const struct fcb *fcb, const struct flash_sector *sector) { - const struct flash_area *fa; int rc; - fa = fcb_open_flash(fcb); - if (fa == NULL) { + if (fcb->fap == NULL) { return FCB_ERR_FLASH; } - rc = flash_area_erase(fa, sector->fs_off, sector->fs_size); - flash_area_close(fa); + rc = flash_area_erase(fcb->fap, sector->fs_off, sector->fs_size); if (rc != 0) { return FCB_ERR_FLASH; @@ -115,7 +89,7 @@ fcb_erase_sector(const struct fcb *fcb, const struct flash_sector *sector) } int -fcb_init(struct fcb *fcb) +fcb_init(int f_area_id, struct fcb *fcb) { struct flash_sector *sector; int rc; @@ -129,8 +103,8 @@ fcb_init(struct fcb *fcb) return FCB_ERR_ARGS; } - fcb->fap = fcb_open_flash(fcb); - if (fcb->fap == NULL) { + rc = flash_area_open(f_area_id, &fcb->fap); + if (rc != 0) { return FCB_ERR_ARGS; } diff --git a/tests/subsys/fs/fcb/src/fcb_test_init.c b/tests/subsys/fs/fcb/src/fcb_test_init.c index 7d8a3c5638..f4fedd4269 100644 --- a/tests/subsys/fs/fcb/src/fcb_test_init.c +++ b/tests/subsys/fs/fcb/src/fcb_test_init.c @@ -14,22 +14,21 @@ void fcb_test_init(void) fcb = &test_fcb; memset(fcb, 0, sizeof(*fcb)); - fcb->f_area_id = TEST_FCB_FLASH_AREA_ID; - rc = fcb_init(fcb); + rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb); zassert_true(rc == FCB_ERR_ARGS, "fcb_init call should fail"); fcb->f_sectors = test_fcb_sector; - rc = fcb_init(fcb); + rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb); zassert_true(rc == FCB_ERR_ARGS, "fcb_init call should fail"); fcb->f_sector_cnt = 2; fcb->f_magic = 0x12345678; - rc = fcb_init(fcb); + rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb); zassert_true(rc == FCB_ERR_MAGIC, "fcb_init call should fail"); fcb->f_magic = 0; - rc = fcb_init(fcb); + rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb); zassert_true(rc == 0, "fcb_init call failure"); } diff --git a/tests/subsys/fs/fcb/src/fcb_test_reset.c b/tests/subsys/fs/fcb/src/fcb_test_reset.c index 4aa97da632..2a48d55ca7 100644 --- a/tests/subsys/fs/fcb/src/fcb_test_reset.c +++ b/tests/subsys/fs/fcb/src/fcb_test_reset.c @@ -61,9 +61,8 @@ void fcb_test_reset(void) memset(fcb, 0, sizeof(*fcb)); fcb->f_sector_cnt = 2; fcb->f_sectors = test_fcb_sector; - fcb->f_area_id = TEST_FCB_FLASH_AREA_ID; - rc = fcb_init(fcb); + rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb); zassert_true(rc == 0, "fcb_init call failure"); var_cnt = 32; @@ -100,9 +99,8 @@ void fcb_test_reset(void) memset(fcb, 0, sizeof(*fcb)); fcb->f_sector_cnt = 2; fcb->f_sectors = test_fcb_sector; - fcb->f_area_id = TEST_FCB_FLASH_AREA_ID; - rc = fcb_init(fcb); + rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb); zassert_true(rc == 0, "fcb_init call failure"); /* diff --git a/tests/subsys/fs/fcb/src/main.c b/tests/subsys/fs/fcb/src/main.c index f4a92e8807..a6ade02154 100644 --- a/tests/subsys/fs/fcb/src/main.c +++ b/tests/subsys/fs/fcb/src/main.c @@ -109,10 +109,9 @@ void fcb_tc_pretest(int sectors) memset(fcb, 0, sizeof(*fcb)); fcb->f_sector_cnt = sectors; fcb->f_sectors = test_fcb_sector; /* XXX */ - fcb->f_area_id = TEST_FCB_FLASH_AREA_ID; rc = 0; - rc = fcb_init(fcb); + rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb); if (rc != 0) { printf("%s rc == %xm, %d\n", __func__, rc, rc); zassert_true(rc == 0, "fbc initialization failure");