Skip to content

Commit e6c4f26

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. Also removes the member `cb_registred` in the struct `bm_zms_init_flags` since it is not used anymore. Signed-off-by: Mirko Covizzi <mirko.covizzi@nordicsemi.no>
1 parent 48e721c commit e6c4f26

File tree

4 files changed

+17
-37
lines changed

4 files changed

+17
-37
lines changed

doc/nrf-bm/release_notes/release_notes_changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ 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+
* Removed the :c:member:`bm_zms_init_flags.cb_registred` member since it was not used anymore.
137+
135138
* :ref:`lib_peer_manager` library:
136139

137140
* Updated:

include/bm/fs/bm_zms.h

Lines changed: 10 additions & 12 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
*/
@@ -60,9 +60,15 @@ typedef struct {
6060
struct bm_zms_init_flags {
6161
volatile bool initialized; /* true when the storage is initialized. */
6262
volatile bool initializing; /* true when initialization is ongoing. */
63-
bool cb_registred; /* true when the user callback is registred. */
6463
} __packed;
6564

65+
/**
66+
* @brief Bare Metal ZMS event handler function prototype.
67+
*
68+
* @param p_evt The event.
69+
*/
70+
typedef void (*bm_zms_cb_t)(bm_zms_evt_t const *p_evt);
71+
6672
/** Zephyr Memory Storage file system structure */
6773
struct bm_zms_fs {
6874
/** File system offset in flash. */
@@ -91,8 +97,8 @@ struct bm_zms_fs {
9197
struct bm_storage zms_bm_storage;
9298
/** Number of writes currently handled by the storage system. */
9399
atomic_t ongoing_writes;
94-
/** The user number that identifies the callback for an event. */
95-
uint32_t user_num;
100+
/** User callback for propagating events. */
101+
bm_zms_cb_t user_cb;
96102
#if CONFIG_BM_ZMS_LOOKUP_CACHE
97103
/** Lookup table used to cache ATE addresses of written IDs. */
98104
uint64_t lookup_cache[CONFIG_BM_ZMS_LOOKUP_CACHE_SIZE];
@@ -121,13 +127,6 @@ struct bm_zms_fs_config {
121127
* @{
122128
*/
123129

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-
131130
/**
132131
* @brief Register a callback to BM_ZMS for handling events.
133132
*
@@ -136,7 +135,6 @@ typedef void (*bm_zms_cb_t)(bm_zms_evt_t const *p_evt);
136135
*
137136
* @retval 0 on success.
138137
* @retval -EFAULT if @p fs or @p cb are NULL.
139-
* @retval -ENOMEM if no more callback slots are available.
140138
*/
141139
int bm_zms_register(struct bm_zms_fs *fs, bm_zms_cb_t cb);
142140

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 & 19 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,23 +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;
344-
fs->init_flags.cb_registred = true;
329+
fs->user_cb = cb;
345330

346331
return 0;
347332
}

0 commit comments

Comments
 (0)