Skip to content

Commit aa2458f

Browse files
committed
subsys: fs: bm_zms: remove global callbacks array
Removes global callbacks array and replaces with callback member inside `struct bm_zms_fs`. This fixes a bug where registering multiple callbacks on the same `struct bm_zms_fs` would update the user reference number incrementally and saturate the global callbacks array. When calling `bm_zms_clear`, this would only reset to NULL the last callback set, leaving the rest still set. This update makes it so that there can only be one callback per each `struct bm_zms_fs` instance, mirroring the behavior of other libraries. Due to this change, also removes the `BM_ZMS_MAX_USERS` Kconfig option. Signed-off-by: Mirko Covizzi <[email protected]>
1 parent 48e721c commit aa2458f

File tree

4 files changed

+16
-34
lines changed

4 files changed

+16
-34
lines changed

doc/nrf-bm/release_notes/release_notes_changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ Libraries
132132
* The :c:func:`bm_zms_active_sector_free_space` function to return ``-EFAULT`` when the input parameter ``fs`` is ``NULL``.
133133
* 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.
134134

135+
* Removed the :kconfig:option:`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`.
136+
135137
* :ref:`lib_peer_manager` library:
136138

137139
* Updated:

include/bm/fs/bm_zms.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extern "C" {
2525
*/
2626

2727
/**
28-
* @defgroup bm_zms_data_structures BM_ZMS data structures
28+
* @defgroup bm_zms_data_types BM_ZMS data types
2929
* @ingroup bm_zms
3030
* @{
3131
*/
@@ -63,6 +63,13 @@ struct bm_zms_init_flags {
6363
bool cb_registred; /* true when the user callback is registred. */
6464
} __packed;
6565

66+
/**
67+
* @brief Bare Metal ZMS event handler function prototype.
68+
*
69+
* @param p_evt The event.
70+
*/
71+
typedef void (*bm_zms_cb_t)(bm_zms_evt_t const *p_evt);
72+
6673
/** Zephyr Memory Storage file system structure */
6774
struct bm_zms_fs {
6875
/** File system offset in flash. */
@@ -91,8 +98,8 @@ struct bm_zms_fs {
9198
struct bm_storage zms_bm_storage;
9299
/** Number of writes currently handled by the storage system. */
93100
atomic_t ongoing_writes;
94-
/** The user number that identifies the callback for an event. */
95-
uint32_t user_num;
101+
/** User callback for propagating events. */
102+
bm_zms_cb_t user_cb;
96103
#if CONFIG_BM_ZMS_LOOKUP_CACHE
97104
/** Lookup table used to cache ATE addresses of written IDs. */
98105
uint64_t lookup_cache[CONFIG_BM_ZMS_LOOKUP_CACHE_SIZE];
@@ -121,13 +128,6 @@ struct bm_zms_fs_config {
121128
* @{
122129
*/
123130

124-
/**
125-
*@brief Bare Metal ZMS event handler function prototype.
126-
*
127-
* @param p_evt The event.
128-
*/
129-
typedef void (*bm_zms_cb_t)(bm_zms_evt_t const *p_evt);
130-
131131
/**
132132
* @brief Register a callback to BM_ZMS for handling events.
133133
*

subsys/fs/bm_zms/Kconfig

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ config BM_ZMS_CUSTOM_BLOCK_SIZE
5050
help
5151
Changes the internal buffer size of BM_ZMS.
5252

53-
config BM_ZMS_MAX_USERS
54-
int "BM_ZMS maximum users"
55-
default 8
56-
help
57-
Defines the maximum number of event handlers that can be registred by the user.
58-
5953
config BM_ZMS_OP_QUEUE_SIZE
6054
int "BM_ZMS operations queue size"
6155
default 16

subsys/fs/bm_zms/bm_zms.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ static int bm_zms_clear_execute(void);
5151
*/
5252
static atomic_t queued_op_cnt;
5353

54-
/* The table of app callback functions. */
55-
static bm_zms_cb_t zms_cb_table[CONFIG_BM_ZMS_MAX_USERS];
56-
5754
/* Queue of bm_zms operations. */
5855
RING_BUF_DECLARE(zms_fifo, CONFIG_BM_ZMS_OP_QUEUE_SIZE * sizeof(zms_op_t));
5956

@@ -97,8 +94,8 @@ static void event_prepare(bm_zms_evt_t *p_evt)
9794

9895
static void event_send(bm_zms_evt_t const *const p_evt, struct bm_zms_fs *fs)
9996
{
100-
if (zms_cb_table[fs->user_num] != NULL) {
101-
zms_cb_table[fs->user_num](p_evt);
97+
if (fs->user_cb != NULL) {
98+
fs->user_cb(p_evt);
10299
}
103100
}
104101

@@ -188,7 +185,7 @@ static void queue_process(void)
188185
/* bm_zms needs to be reinitialized after clearing */
189186
cur_op.fs->init_flags.initialized = false;
190187
cur_op.fs->init_flags.initializing = false;
191-
zms_cb_table[cur_op.fs->user_num] = NULL;
188+
cur_op.fs->user_cb = NULL;
192189
cur_op.op_completed = true;
193190
result = 0;
194191
} else {
@@ -325,22 +322,11 @@ static void zms_event_handler(struct bm_storage_evt *p_evt)
325322

326323
int bm_zms_register(struct bm_zms_fs *fs, bm_zms_cb_t cb)
327324
{
328-
int i;
329-
330325
if (!fs || !cb) {
331326
return -EFAULT;
332327
}
333328

334-
for (i = 0; i < CONFIG_BM_ZMS_MAX_USERS; i++) {
335-
if (zms_cb_table[i] == NULL) {
336-
break;
337-
}
338-
}
339-
if (i == CONFIG_BM_ZMS_MAX_USERS) {
340-
return -ENOMEM;
341-
}
342-
zms_cb_table[i] = cb;
343-
fs->user_num = i;
329+
fs->user_cb = cb;
344330
fs->init_flags.cb_registred = true;
345331

346332
return 0;

0 commit comments

Comments
 (0)