Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion doc/nrf-bm/release_notes/release_notes_changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ Libraries

* Updated:

* The :c:func:`bm_zms_register` function to return ``-EFAULT`` instead of ``-EINVAL`` when the input parameters are ``NULL``.
* The :c:func:`bm_zms_mount` function to return ``-EFAULT`` when the input parameter ``fs`` is ``NULL``.
* The :c:func:`bm_zms_clear` function to return ``-EFAULT`` when the input parameter ``fs`` is ``NULL``.
* The :c:func:`bm_zms_write` function to return ``-EFAULT`` when the input parameter ``fs`` is ``NULL``.
Expand All @@ -136,12 +135,17 @@ Libraries
* The :c:func:`bm_zms_calc_free_space` function to return ``-EFAULT`` when the input parameter ``fs`` is ``NULL``.
* The :c:func:`bm_zms_active_sector_free_space` function to return ``-EFAULT`` when the input parameter ``fs`` is ``NULL``.
* The :c:func:`bm_zms_mount` function to expect an additional input parameter of type pointer to struct :c:struct:`bm_zms_fs_config` for configuring a Zephyr Memory Storage file system instance at initialization.
* By renaming the type ``bm_zms_evt_id_t`` to enum :c:enum:`bm_zms_evt_type`.
* By renaming the type ``bm_zms_evt_t`` to struct :c:struct:`bm_zms_evt`.
* By renaming the event ``BM_ZMS_EVT_INIT`` to :c:enum:`BM_ZMS_EVT_MOUNT`.

* Removed:

* The ``CONFIG_BM_ZMS_MAX_USERS`` Kconfig option.
Now the library expects at most one callback for each instance of the struct :c:struct:`bm_zms_fs`.
* The ``bm_zms_init_flags.cb_registred`` member as it was not used anymore.
* The ``bm_zms_register`` function.
The event handler configuration is now done with the struct :c:struct:`bm_zms_fs_config`.

* :ref:`lib_peer_manager` library:

Expand Down
78 changes: 39 additions & 39 deletions include/bm/fs/bm_zms.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,39 @@ extern "C" {
*/

/** BM_ZMS event IDs. */
typedef enum {
BM_ZMS_EVT_NONE, /* Event if an internal error happened before queuing an operation. */
BM_ZMS_EVT_INIT, /* Event for @ref bm_zms_init. */
BM_ZMS_EVT_WRITE, /* Event for @ref bm_zms_write. */
BM_ZMS_EVT_DELETE, /* Event for @ref bm_zms_delete. */
BM_ZMS_EVT_CLEAR, /* Event for @ref bm_zms_clear. */
} bm_zms_evt_id_t;
enum bm_zms_evt_type {
/** Event if an internal error happened before queuing an operation. */
BM_ZMS_EVT_NONE,
/** Event for @ref bm_zms_mount. */
BM_ZMS_EVT_MOUNT,
/** Event for @ref bm_zms_write. */
BM_ZMS_EVT_WRITE,
/** Event for @ref bm_zms_delete. */
BM_ZMS_EVT_DELETE,
/** Event for @ref bm_zms_clear. */
BM_ZMS_EVT_CLEAR,
};

/**@brief A BM_ZMS event. */
typedef struct {
bm_zms_evt_id_t evt_id; /* The event ID. See @ref bm_zms_evt_id_t. */
int result; /* The result of the operation related to this event.
* The operation might have failed for one of the following reasons:
* -ENOSPC: There is no free space in flash.
* -EIO: Internal BM_ZMS error.
* -ENOTSUP: The BM_ZMS version is not supported.
* -ENOEXEC: Bad BM_ZMS format.
* -EFAULT: Bad sector layout.
* -ENOMEM: The internal queue buffer is full.
*/
uint32_t id; /* The ID of the entry as specified in the corresponding
* write/delete operation.
*/
} bm_zms_evt_t;
struct bm_zms_evt {
/** The event type. See @ref bm_zms_evt_type. */
enum bm_zms_evt_type evt_type;
/** The result of the operation related to this event.
* The operation might have failed for one of the following
* reasons:
* -ENOSPC: There is no free space in non-volatile storage.
* -EIO: Internal BM_ZMS error.
* -ENOTSUP: The BM_ZMS version is not supported.
* -ENOEXEC: Bad BM_ZMS format.
* -EFAULT: Bad sector layout.
* -ENOMEM: The internal queue buffer is full.
*/
int result;
/** The ID of the entry as specified in the corresponding
* write/delete operation.
*/
uint32_t id;
};

/* Init flags. */
struct bm_zms_init_flags {
Expand All @@ -65,13 +74,13 @@ struct bm_zms_init_flags {
/**
* @brief Bare Metal ZMS event handler function prototype.
*
* @param p_evt The event.
* @param evt The event.
*/
typedef void (*bm_zms_cb_t)(bm_zms_evt_t const *p_evt);
typedef void (*bm_zms_evt_handler_t)(struct bm_zms_evt const *evt);

/** Zephyr Memory Storage file system structure */
struct bm_zms_fs {
/** File system offset in flash. */
/** File system offset in non-volatile storage. */
off_t offset;
/** Allocation Table Entry (ATE) write address.
* Addresses are stored as `uint64_t`:
Expand All @@ -97,8 +106,8 @@ struct bm_zms_fs {
struct bm_storage zms_bm_storage;
/** Number of writes currently handled by the storage system. */
atomic_t ongoing_writes;
/** User callback for propagating events. */
bm_zms_cb_t user_cb;
/** Event handler for propagating events. */
bm_zms_evt_handler_t evt_handler;
#if CONFIG_BM_ZMS_LOOKUP_CACHE
/** Lookup table used to cache ATE addresses of written IDs. */
uint64_t lookup_cache[CONFIG_BM_ZMS_LOOKUP_CACHE_SIZE];
Expand All @@ -107,14 +116,16 @@ struct bm_zms_fs {

/** Configuration for Zephyr Memory Storage file system structure initialization. */
struct bm_zms_fs_config {
/** File system offset in flash. */
/** File system offset in non-volatile storage. */
off_t offset;
/** Storage system is split into sectors. The sector size must be a multiple of
* `erase-block-size` if the device has erase capabilities.
*/
uint32_t sector_size;
/** Number of sectors in the file system. */
uint32_t sector_count;
/** Event handler for propagating events. */
bm_zms_evt_handler_t evt_handler;
};

/**
Expand All @@ -127,17 +138,6 @@ struct bm_zms_fs_config {
* @{
*/

/**
* @brief Register a callback to BM_ZMS for handling events.
*
* @param fs Pointer to the file system structure.
* @param cb Pointer to the event handler callback.
*
* @retval 0 on success.
* @retval -EFAULT if @p fs or @p cb are NULL.
*/
int bm_zms_register(struct bm_zms_fs *fs, bm_zms_cb_t cb);

/**
* @brief Mount a BM_ZMS file system.
*
Expand Down
13 changes: 4 additions & 9 deletions lib/bluetooth/peer_manager/modules/peer_data_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ static void peer_ids_load(void)
}
}

static void bm_zms_evt_handler(const bm_zms_evt_t *evt)
static void bm_zms_evt_handler(const struct bm_zms_evt *evt)
{
uint16_t peer_id;
enum pm_peer_data_id data_id;
Expand All @@ -195,8 +195,8 @@ static void bm_zms_evt_handler(const bm_zms_evt_t *evt)

struct pm_evt pds_evt = { .peer_id = peer_id };

switch (evt->evt_id) {
case BM_ZMS_EVT_INIT:
switch (evt->evt_type) {
case BM_ZMS_EVT_MOUNT:
if (evt->result) {
LOG_ERR("BM_ZMS initialization failed with error %d", evt->result);
}
Expand Down Expand Up @@ -345,16 +345,11 @@ uint32_t pds_init(void)
/* Check for re-initialization if debugging. */
__ASSERT_NO_MSG(!module_initialized);

err = bm_zms_register(&fs, bm_zms_evt_handler);
if (err) {
LOG_ERR("Could not initialize NVM storage. bm_zms_register() returned %d.", err);
return NRF_ERROR_INTERNAL;
}

struct bm_zms_fs_config config = {
.offset = PEER_MANAGER_PARTITION_OFFSET,
.sector_size = CONFIG_PM_BM_ZMS_SECTOR_SIZE,
.sector_count = (PEER_MANAGER_PARTITION_SIZE / CONFIG_PM_BM_ZMS_SECTOR_SIZE),
.evt_handler = bm_zms_evt_handler,
};

err = bm_zms_mount(&fs, &config);
Expand Down
25 changes: 10 additions & 15 deletions samples/subsys/fs/bm_zms/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,24 @@ static int delete_basic_items(struct bm_zms_fs *fs)
return rc;
}

void bm_zms_sample_handler(bm_zms_evt_t const *p_evt)
void bm_zms_sample_handler(struct bm_zms_evt const *evt)
{
if (p_evt->evt_id == BM_ZMS_EVT_INIT) {
if (p_evt->result) {
LOG_ERR("BM_ZMS initialization failed with error %d", p_evt->result);
if (evt->evt_type == BM_ZMS_EVT_MOUNT) {
if (evt->result) {
LOG_ERR("BM_ZMS initialization failed with error %d", evt->result);
return;
}
} else if ((p_evt->evt_id == BM_ZMS_EVT_WRITE) || (p_evt->evt_id == BM_ZMS_EVT_DELETE)) {
if (!p_evt->result) {
} else if ((evt->evt_type == BM_ZMS_EVT_WRITE) || (evt->evt_type == BM_ZMS_EVT_DELETE)) {
if (!evt->result) {
return;
}
if (p_evt->result == -ENOSPC) {
if (evt->result == -ENOSPC) {
nvm_is_full = true;
return;
}
LOG_ERR("BM_ZMS Error received %d", p_evt->result);
LOG_ERR("BM_ZMS Error received %d", evt->result);
} else {
LOG_WRN("Unhandled BM_ZMS event ID %u", p_evt->evt_id);
LOG_WRN("Unhandled BM_ZMS event ID %u", evt->evt_type);
}
}

Expand All @@ -154,16 +154,11 @@ int main(void)
longarray[n] = n;
}

rc = bm_zms_register(&fs, bm_zms_sample_handler);
if (rc) {
LOG_ERR("Something is wrong %u", rc);
goto idle;
}

struct bm_zms_fs_config config = {
.offset = BM_ZMS_PARTITION_OFFSET,
.sector_size = CONFIG_APP_BM_ZMS_SECTOR_SIZE,
.sector_count = (BM_ZMS_PARTITION_SIZE / CONFIG_APP_BM_ZMS_SECTOR_SIZE),
.evt_handler = bm_zms_sample_handler
};

for (i = 0; i < CONFIG_APP_BM_ZMS_ITERATIONS_MAX; i++) {
Expand Down
34 changes: 12 additions & 22 deletions subsys/fs/bm_zms/bm_zms.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,37 +65,37 @@ static int zms_get_sector_header(struct bm_zms_fs *fs, uint64_t addr, struct zms
static int zms_ate_valid_different_sector(struct bm_zms_fs *fs, const struct zms_ate *entry,
uint8_t cycle_cnt);

static void event_prepare(bm_zms_evt_t *p_evt)
static void event_prepare(struct bm_zms_evt *evt)
{
switch (cur_op.op_code) {
case ZMS_OP_INIT:
p_evt->evt_id = BM_ZMS_EVT_INIT;
evt->evt_type = BM_ZMS_EVT_MOUNT;
break;

case ZMS_OP_WRITE:
atomic_sub(&cur_op.fs->ongoing_writes, 1);
p_evt->evt_id = (!cur_op.data_len && !cur_op.data) ? BM_ZMS_EVT_DELETE :
evt->evt_type = (!cur_op.data_len && !cur_op.data) ? BM_ZMS_EVT_DELETE :
BM_ZMS_EVT_WRITE;
p_evt->id = cur_op.id;
evt->id = cur_op.id;
break;

case ZMS_OP_CLEAR:
p_evt->evt_id = BM_ZMS_EVT_CLEAR;
evt->evt_type = BM_ZMS_EVT_CLEAR;
break;

case ZMS_OP_NONE:
p_evt->evt_id = BM_ZMS_EVT_NONE;
evt->evt_type = BM_ZMS_EVT_NONE;
break;
default:
/* Should not happen. */
break;
}
}

static void event_send(bm_zms_evt_t const *const p_evt, struct bm_zms_fs *fs)
static void event_send(struct bm_zms_evt const *const evt, struct bm_zms_fs *fs)
{
if (fs->user_cb != NULL) {
fs->user_cb(p_evt);
if (fs->evt_handler != NULL) {
fs->evt_handler(evt);
}
}

Expand Down Expand Up @@ -185,7 +185,7 @@ static void queue_process(void)
/* bm_zms needs to be reinitialized after clearing */
cur_op.fs->init_flags.initialized = false;
cur_op.fs->init_flags.initializing = false;
cur_op.fs->user_cb = NULL;
cur_op.fs->evt_handler = NULL;
cur_op.op_completed = true;
result = 0;
} else {
Expand Down Expand Up @@ -220,7 +220,7 @@ static void queue_process(void)
/* no errors. */
evt_result = 0;
}
bm_zms_evt_t evt = {
struct bm_zms_evt evt = {
/* The operation might have failed for one of the following reasons:
* -ENOSPC: no free space in flash.
* -EIO: Internal BM_ZMS error.
Expand Down Expand Up @@ -320,17 +320,6 @@ static void zms_event_handler(struct bm_storage_evt *p_evt)
}
}

int bm_zms_register(struct bm_zms_fs *fs, bm_zms_cb_t cb)
{
if (!fs || !cb) {
return -EFAULT;
}

fs->user_cb = cb;

return 0;
}

#ifdef CONFIG_BM_ZMS_LOOKUP_CACHE

static inline size_t zms_lookup_cache_pos(uint32_t id)
Expand Down Expand Up @@ -2009,6 +1998,7 @@ int bm_zms_mount(struct bm_zms_fs *fs, const struct bm_zms_fs_config *config)
fs->offset = config->offset;
fs->sector_size = config->sector_size;
fs->sector_count = config->sector_count;
fs->evt_handler = config->evt_handler;

/* Initialize BM Storage */

Expand Down
Loading