fcb: start using errno codes

Switch form using privater FCB error codes to
errno codes. FCB private codes convention were compatible
with <errno.h> codes:
- 0 mean success
- negative values mean errors
- similar error types.
There was no sense to kept private FCB error codes.

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
This commit is contained in:
Andrzej Puzdrowski 2019-07-11 11:21:35 +02:00 committed by Carles Cufí
commit 94a022c954
14 changed files with 56 additions and 66 deletions

View file

@ -123,18 +123,6 @@ struct fcb {
*/
};
/*
* Error codes.
*/
#define FCB_OK 0
#define FCB_ERR_ARGS -1
#define FCB_ERR_FLASH -2
#define FCB_ERR_NOVAR -3
#define FCB_ERR_NOSPACE -4
#define FCB_ERR_NOMEM -5
#define FCB_ERR_CRC -6
#define FCB_ERR_MAGIC -7
/**
* @}
*/

View file

@ -11,6 +11,7 @@
#include <fs/fcb.h>
#include "fcb_priv.h"
#include "string.h"
#include <errno.h>
u8_t
fcb_get_align(const struct fcb *fcb)
@ -32,17 +33,17 @@ int fcb_flash_read(const struct fcb *fcb, const struct flash_sector *sector,
int rc;
if (off + len > sector->fs_size) {
return FCB_ERR_ARGS;
return -EINVAL;
}
if (fcb->fap == NULL) {
return FCB_ERR_FLASH;
return -EIO;
}
rc = flash_area_read(fcb->fap, sector->fs_off + off, dst, len);
if (rc != 0) {
return FCB_ERR_FLASH;
return -EIO;
}
return 0;
@ -54,17 +55,17 @@ int fcb_flash_write(const struct fcb *fcb, const struct flash_sector *sector,
int rc;
if (off + len > sector->fs_size) {
return FCB_ERR_ARGS;
return -EINVAL;
}
if (fcb->fap == NULL) {
return FCB_ERR_FLASH;
return -EIO;
}
rc = flash_area_write(fcb->fap, sector->fs_off + off, src, len);
if (rc != 0) {
return FCB_ERR_FLASH;
return -EIO;
}
return 0;
@ -76,13 +77,13 @@ fcb_erase_sector(const struct fcb *fcb, const struct flash_sector *sector)
int rc;
if (fcb->fap == NULL) {
return FCB_ERR_FLASH;
return -EIO;
}
rc = flash_area_erase(fcb->fap, sector->fs_off, sector->fs_size);
if (rc != 0) {
return FCB_ERR_FLASH;
return -EIO;
}
return 0;
@ -100,17 +101,17 @@ fcb_init(int f_area_id, struct fcb *fcb)
struct fcb_disk_area fda;
if (!fcb->f_sectors || fcb->f_sector_cnt - fcb->f_scratch_cnt < 1) {
return FCB_ERR_ARGS;
return -EINVAL;
}
rc = flash_area_open(f_area_id, &fcb->fap);
if (rc != 0) {
return FCB_ERR_ARGS;
return -EINVAL;
}
align = fcb_get_align(fcb);
if (align == 0U) {
return FCB_ERR_ARGS;
return -EINVAL;
}
/* Fill last used, first used */
@ -155,8 +156,8 @@ fcb_init(int f_area_id, struct fcb *fcb)
while (1) {
rc = fcb_getnext_in_sector(fcb, &fcb->f_active);
if (rc == FCB_ERR_NOVAR) {
rc = FCB_OK;
if (rc == -ENOTSUP) {
rc = 0;
break;
}
if (rc != 0) {
@ -205,7 +206,7 @@ fcb_put_len(u8_t *buf, u16_t len)
buf[1] = len >> 7;
return 2;
} else {
return FCB_ERR_ARGS;
return -EINVAL;
}
}
@ -216,7 +217,7 @@ fcb_get_len(u8_t *buf, u16_t *len)
if (buf[0] & 0x80) {
if (buf[0] == 0xff && buf[1] == 0xff) {
return FCB_ERR_NOVAR;
return -ENOTSUP;
}
*len = (buf[0] & 0x7f) | (buf[1] << 7);
rc = 2;
@ -243,7 +244,7 @@ fcb_sector_hdr_init(struct fcb *fcb, struct flash_sector *sector, u16_t id)
rc = fcb_flash_write(fcb, sector, 0, &fda, sizeof(fda));
if (rc != 0) {
return FCB_ERR_FLASH;
return -EIO;
}
return 0;
}
@ -265,13 +266,13 @@ int fcb_sector_hdr_read(struct fcb *fcb, struct flash_sector *sector,
}
rc = fcb_flash_read(fcb, sector, 0, fdap, sizeof(*fdap));
if (rc) {
return FCB_ERR_FLASH;
return -EIO;
}
if (fdap->fd_magic == 0xffffffff) {
return 0;
}
if (fdap->fd_magic != fcb->f_magic) {
return FCB_ERR_MAGIC;
return -ENOMSG;
}
return 1;
}

View file

@ -44,7 +44,7 @@ fcb_append_to_scratch(struct fcb *fcb)
sector = fcb_new_sector(fcb, 0);
if (!sector) {
return FCB_ERR_NOSPACE;
return -ENOSPC;
}
rc = fcb_sector_hdr_init(fcb, sector, fcb->f_active_id + 1);
if (rc) {
@ -53,7 +53,7 @@ fcb_append_to_scratch(struct fcb *fcb)
fcb->f_active.fe_sector = sector;
fcb->f_active.fe_elem_off = sizeof(struct fcb_disk_area);
fcb->f_active_id++;
return FCB_OK;
return 0;
}
int
@ -76,14 +76,14 @@ fcb_append(struct fcb *fcb, u16_t len, struct fcb_entry *append_loc)
rc = k_mutex_lock(&fcb->f_mtx, K_FOREVER);
if (rc) {
return FCB_ERR_ARGS;
return -EINVAL;
}
active = &fcb->f_active;
if (active->fe_elem_off + len + cnt > active->fe_sector->fs_size) {
sector = fcb_new_sector(fcb, fcb->f_scratch_cnt);
if (!sector || (sector->fs_size <
sizeof(struct fcb_disk_area) + len + cnt)) {
rc = FCB_ERR_NOSPACE;
rc = -ENOSPC;
goto err;
}
rc = fcb_sector_hdr_init(fcb, sector, fcb->f_active_id + 1);
@ -97,7 +97,7 @@ fcb_append(struct fcb *fcb, u16_t len, struct fcb_entry *append_loc)
rc = fcb_flash_write(fcb, active->fe_sector, active->fe_elem_off, tmp_str, cnt);
if (rc) {
rc = FCB_ERR_FLASH;
rc = -EIO;
goto err;
}
append_loc->fe_sector = active->fe_sector;
@ -108,7 +108,7 @@ fcb_append(struct fcb *fcb, u16_t len, struct fcb_entry *append_loc)
k_mutex_unlock(&fcb->f_mtx);
return FCB_OK;
return 0;
err:
k_mutex_unlock(&fcb->f_mtx);
return rc;
@ -131,7 +131,7 @@ fcb_append_finish(struct fcb *fcb, struct fcb_entry *loc)
rc = fcb_flash_write(fcb, loc->fe_sector, off, crc8, fcb->f_align);
if (rc) {
return FCB_ERR_FLASH;
return -EIO;
}
return 0;
}

View file

@ -27,11 +27,11 @@ fcb_elem_crc8(struct fcb *fcb, struct fcb_entry *loc, u8_t *c8p)
int rc;
if (loc->fe_elem_off + 2 > loc->fe_sector->fs_size) {
return FCB_ERR_NOVAR;
return -ENOTSUP;
}
rc = fcb_flash_read(fcb, loc->fe_sector, loc->fe_elem_off, tmp_str, 2);
if (rc) {
return FCB_ERR_FLASH;
return -EIO;
}
cnt = fcb_get_len(tmp_str, &len);
@ -54,7 +54,7 @@ fcb_elem_crc8(struct fcb *fcb, struct fcb_entry *loc, u8_t *c8p)
rc = fcb_flash_read(fcb, loc->fe_sector, off, tmp_str, blk_sz);
if (rc) {
return FCB_ERR_FLASH;
return -EIO;
}
crc8 = crc8_ccitt(crc8, tmp_str, blk_sz);
}
@ -78,11 +78,11 @@ int fcb_elem_info(struct fcb *fcb, struct fcb_entry *loc)
rc = fcb_flash_read(fcb, loc->fe_sector, off, &fl_crc8, sizeof(fl_crc8));
if (rc) {
return FCB_ERR_FLASH;
return -EIO;
}
if (fl_crc8 != crc8) {
return FCB_ERR_CRC;
return -EBADMSG;
}
return 0;
}

View file

@ -16,16 +16,16 @@ fcb_getnext_in_sector(struct fcb *fcb, struct fcb_entry *loc)
int rc;
rc = fcb_elem_info(fcb, loc);
if (rc == 0 || rc == FCB_ERR_CRC) {
if (rc == 0 || rc == -EBADMSG) {
do {
loc->fe_elem_off = loc->fe_data_off +
fcb_len_in_flash(fcb, loc->fe_data_len) +
fcb_len_in_flash(fcb, FCB_CRC_SZ);
rc = fcb_elem_info(fcb, loc);
if (rc != FCB_ERR_CRC) {
if (rc != -EBADMSG) {
break;
}
} while (rc == FCB_ERR_CRC);
} while (rc == -EBADMSG);
}
return rc;
}
@ -60,7 +60,7 @@ fcb_getnext_nolock(struct fcb *fcb, struct fcb_entry *loc)
switch (rc) {
case 0:
return 0;
case FCB_ERR_CRC:
case -EBADMSG:
break;
default:
goto next_sector;
@ -70,23 +70,23 @@ fcb_getnext_nolock(struct fcb *fcb, struct fcb_entry *loc)
if (rc == 0) {
return 0;
}
if (rc == FCB_ERR_NOVAR) {
if (rc == -ENOTSUP) {
goto next_sector;
}
}
while (rc == FCB_ERR_CRC) {
while (rc == -EBADMSG) {
rc = fcb_getnext_in_sector(fcb, loc);
if (rc == 0) {
return 0;
}
if (rc != FCB_ERR_CRC) {
if (rc != -EBADMSG) {
/*
* Moving to next sector.
*/
next_sector:
if (loc->fe_sector == fcb->f_active.fe_sector) {
return FCB_ERR_NOVAR;
return -ENOTSUP;
}
loc->fe_sector = fcb_getnext_sector(fcb, loc->fe_sector);
loc->fe_elem_off = sizeof(struct fcb_disk_area);
@ -94,7 +94,7 @@ next_sector:
switch (rc) {
case 0:
return 0;
case FCB_ERR_CRC:
case -EBADMSG:
break;
default:
goto next_sector;
@ -112,7 +112,7 @@ fcb_getnext(struct fcb *fcb, struct fcb_entry *loc)
rc = k_mutex_lock(&fcb->f_mtx, K_FOREVER);
if (rc) {
return FCB_ERR_ARGS;
return -EINVAL;
}
rc = fcb_getnext_nolock(fcb, loc);
k_mutex_unlock(&fcb->f_mtx);

View file

@ -16,12 +16,12 @@ fcb_rotate(struct fcb *fcb)
rc = k_mutex_lock(&fcb->f_mtx, K_FOREVER);
if (rc) {
return FCB_ERR_ARGS;
return -EINVAL;
}
rc = fcb_erase_sector(fcb, fcb->f_oldest);
if (rc) {
rc = FCB_ERR_FLASH;
rc = -EIO;
goto out;
}
if (fcb->f_oldest == fcb->f_active.fe_sector) {

View file

@ -24,10 +24,10 @@ fcb_walk(struct fcb *fcb, struct flash_sector *sector, fcb_walk_cb cb,
rc = k_mutex_lock(&fcb->f_mtx, K_FOREVER);
if (rc < 0) {
return FCB_ERR_ARGS;
return -EINVAL;
}
while ((rc = fcb_getnext_nolock(fcb, &entry_ctx.loc)) !=
FCB_ERR_NOVAR) {
-ENOTSUP) {
k_mutex_unlock(&fcb->f_mtx);
if (sector && entry_ctx.loc.fe_sector != sector) {
return 0;
@ -41,7 +41,7 @@ fcb_walk(struct fcb *fcb, struct flash_sector *sector, fcb_walk_cb cb,
}
rc = k_mutex_lock(&fcb->f_mtx, K_FOREVER);
if (rc < 0) {
return FCB_ERR_ARGS;
return -EINVAL;
}
}
k_mutex_unlock(&fcb->f_mtx);

View file

@ -268,7 +268,7 @@ static int settings_fcb_save_priv(struct settings_store *cs, const char *name,
for (i = 0; i < cf->cf_fcb.f_sector_cnt - 1; i++) {
rc = fcb_append(&cf->cf_fcb, len, &loc.loc);
if (rc != FCB_ERR_NOSPACE) {
if (rc != -ENOSPC) {
break;
}
settings_fcb_compress(cf);

View file

@ -14,6 +14,7 @@
#include <fs/fcb.h>
#include "fcb_priv.h"
#include <errno.h>
#ifdef __cplusplus
#extern "C" {

View file

@ -32,7 +32,7 @@ void fcb_test_append_fill(void)
while (1) {
rc = fcb_append(fcb, sizeof(test_data), &loc);
if (rc == FCB_ERR_NOSPACE) {
if (rc == -ENOSPC) {
break;
}
if (loc.fe_sector == &test_fcb_sector[0]) {

View file

@ -16,17 +16,17 @@ void fcb_test_init(void)
(void)memset(fcb, 0, sizeof(*fcb));
rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb);
zassert_true(rc == FCB_ERR_ARGS, "fcb_init call should fail");
zassert_true(rc == -EINVAL, "fcb_init call should fail");
fcb->f_sectors = test_fcb_sector;
rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb);
zassert_true(rc == FCB_ERR_ARGS, "fcb_init call should fail");
zassert_true(rc == -EINVAL, "fcb_init call should fail");
fcb->f_sector_cnt = 2U;
fcb->f_magic = 0x12345678;
rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb);
zassert_true(rc == FCB_ERR_MAGIC, "fcb_init call should fail");
zassert_true(rc == -ENOMSG, "fcb_init call should fail");
fcb->f_magic = 0U;
rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb);

View file

@ -29,7 +29,7 @@ void fcb_test_last_of_n(void)
*/
for (i = 0U; i < ENTRIES; i++) {
rc = fcb_append(fcb, sizeof(test_data), &loc);
if (rc == FCB_ERR_NOSPACE) {
if (rc == -ENOSPC) {
break;
}

View file

@ -30,7 +30,7 @@ void fcb_test_multi_scratch(void)
(void)memset(elem_cnts, 0, sizeof(elem_cnts));
while (1) {
rc = fcb_append(fcb, sizeof(test_data), &loc);
if (rc == FCB_ERR_NOSPACE) {
if (rc == -ENOSPC) {
break;
}
idx = loc.fe_sector - &test_fcb_sector[0];
@ -58,7 +58,7 @@ void fcb_test_multi_scratch(void)
while (1) {
rc = fcb_append(fcb, sizeof(test_data), &loc);
if (rc == FCB_ERR_NOSPACE) {
if (rc == -ENOSPC) {
break;
}
idx = loc.fe_sector - &test_fcb_sector[0];

View file

@ -33,7 +33,7 @@ void fcb_test_rotate(void)
*/
while (1) {
rc = fcb_append(fcb, sizeof(test_data), &loc);
if (rc == FCB_ERR_NOSPACE) {
if (rc == -ENOSPC) {
break;
}
if (loc.fe_sector == &test_fcb_sector[0]) {