Skip to content

Commit 4e8fde1

Browse files
nordicjmcarlescufi
authored andcommitted
mgmt: mcumgr: fs_mgmt: Switch to new event callback system
Switches to the new event callback system for the fs_mgmt functionality and removes the old code. Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
1 parent 042ed05 commit 4e8fde1

File tree

5 files changed

+121
-53
lines changed

5 files changed

+121
-53
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2022 Laird Connectivity
3+
* Copyright (c) 2022 Nordic Semiconductor ASA
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#ifndef H_MCUMGR_FS_MGMT_CALLBACKS_
9+
#define H_MCUMGR_FS_MGMT_CALLBACKS_
10+
11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
14+
15+
/**
16+
* @brief MCUmgr fs_mgmt callback API
17+
* @defgroup mcumgr_callback_api_fs_mgmt MCUmgr fs_mgmt callback API
18+
* @ingroup mcumgr_callback_api
19+
* @{
20+
*/
21+
22+
/**
23+
* Structure provided in the MGMT_EVT_OP_FS_MGMT_FILE_ACCESS notification callback: This callback
24+
* function is used to notify the application about a pending file read/write request and to
25+
* authorise or deny it. Access will be allowed so long as all notification handlers return
26+
* MGMT_ERR_EOK, if one returns an error then access will be denied.
27+
*/
28+
struct fs_mgmt_file_access {
29+
/** True if the request is for uploading data to the file, otherwise false */
30+
bool upload;
31+
32+
/**
33+
* Path and filename of file be accesses, note that this can be changed by handlers to
34+
* redirect file access if needed (as long as it does not exceed the maximum path string
35+
* size).
36+
*/
37+
char *filename;
38+
};
39+
40+
/**
41+
* @}
42+
*/
43+
44+
#ifdef __cplusplus
45+
}
46+
#endif
47+
48+
#endif

include/zephyr/mgmt/mcumgr/mgmt/callbacks.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include <zephyr/sys/slist.h>
1212
#include <mgmt/mgmt.h>
1313

14+
#ifdef CONFIG_MCUMGR_CMD_FS_MGMT
15+
#include <zephyr/mgmt/mcumgr/grp/fs_mgmt/fs_mgmt_callbacks.h>
16+
#endif
17+
1418
#ifdef __cplusplus
1519
extern "C" {
1620
#endif
@@ -66,6 +70,8 @@ typedef int32_t (*mgmt_cb)(uint32_t event, int32_t rc, bool *abort_more, void *d
6670
enum mgmt_cb_groups {
6771
MGMT_EVT_GRP_ALL = 0,
6872
MGMT_EVT_GRP_SMP,
73+
MGMT_EVT_GRP_IMG,
74+
MGMT_EVT_GRP_FS,
6975

7076
MGMT_EVT_GRP_USER_CUSTOM_START = MGMT_GROUP_ID_PERUSER,
7177
};
@@ -95,6 +101,40 @@ enum smp_group_events {
95101
MGMT_EVT_OP_CMD_ALL = MGMT_DEF_EVT_OP_ALL(MGMT_EVT_GRP_SMP),
96102
};
97103

104+
/**
105+
* MGMT event opcodes for filesystem management group.
106+
*/
107+
enum fs_mgmt_group_events {
108+
/** Callback when a file has been accessed, data is fs_mgmt_file_access. */
109+
MGMT_EVT_OP_FS_MGMT_FILE_ACCESS = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_FS, 0),
110+
111+
/** Used to enable all fs_mgmt_group events. */
112+
MGMT_EVT_OP_FS_MGMT_ALL = MGMT_DEF_EVT_OP_ALL(MGMT_EVT_GRP_FS),
113+
};
114+
115+
/**
116+
* MGMT event opcodes for image management group.
117+
*/
118+
enum img_mgmt_group_events {
119+
/** Callback when a client sends a file upload chunk, data is img_mgmt_upload_check. */
120+
MGMT_EVT_OP_IMG_MGMT_DFU_CHUNK = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_IMG, 0),
121+
122+
/** Callback when a DFU operation is stopped. */
123+
MGMT_EVT_OP_IMG_MGMT_DFU_STOPPED = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_IMG, 1),
124+
125+
/** Callback when a DFU operation is started. */
126+
MGMT_EVT_OP_IMG_MGMT_DFU_STARTED = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_IMG, 2),
127+
128+
/** Callback when a DFU operation has finished being transferred. */
129+
MGMT_EVT_OP_IMG_MGMT_DFU_PENDING = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_IMG, 3),
130+
131+
/** Callback when an image has been confirmed. */
132+
MGMT_EVT_OP_IMG_MGMT_DFU_CONFIRMED = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_IMG, 4),
133+
134+
/** Used to enable all img_mgmt_group events. */
135+
MGMT_EVT_OP_IMG_MGMT_ALL = MGMT_DEF_EVT_OP_ALL(MGMT_EVT_GRP_IMG),
136+
};
137+
98138
/**
99139
* MGMT callback struct
100140
*/

subsys/mgmt/mcumgr/lib/cmd/fs_mgmt/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ config FS_MGMT_PATH_SIZE
140140
of this size gets allocated on the stack during handling of file upload
141141
and download commands.
142142

143-
config FS_MGMT_FILE_ACCESS_HOOK
143+
config MCUMGR_GRP_FS_FILE_ACCESS_HOOK
144144
bool "File read/write access hook"
145+
depends on MCUMGR_MGMT_NOTIFICATION_HOOKS
145146
help
146147
Allows applications to control file read and write access by
147148
registering for a callback which is then triggered whenever a file

subsys/mgmt/mcumgr/lib/cmd/fs_mgmt/include/fs_mgmt/fs_mgmt.h

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Copyright (c) 2018-2022 mcumgr authors
33
* Copyright (c) 2022 Laird Connectivity
4+
* Copyright (c) 2022 Nordic Semiconductor ASA
45
*
56
* SPDX-License-Identifier: Apache-2.0
67
*/
@@ -20,38 +21,11 @@ extern "C" {
2021
#define FS_MGMT_ID_HASH_CHECKSUM 2
2122
#define FS_MGMT_ID_SUPPORTED_HASH_CHECKSUM 3
2223

23-
#ifdef CONFIG_FS_MGMT_FILE_ACCESS_HOOK
24-
/** @typedef fs_mgmt_on_evt_cb
25-
* @brief Function to be called on fs mgmt event.
26-
*
27-
* This callback function is used to notify the application about a pending file
28-
* read/write request and to authorise or deny it.
29-
*
30-
* @param write True if write access is requested, false for read access
31-
* @param path The path of the file to query.
32-
*
33-
* @note That the path can potentially be changed by the application code so
34-
* long as it does not exceed the maximum path string size.
35-
*
36-
* @return 0 to allow read/write, MGMT_ERR_[...] code to disallow read/write.
37-
*/
38-
typedef int (*fs_mgmt_on_evt_cb)(bool write, char *path);
39-
#endif
40-
4124
/**
4225
* @brief Registers the file system management command handler group.
4326
*/
4427
void fs_mgmt_register_group(void);
4528

46-
#ifdef CONFIG_FS_MGMT_FILE_ACCESS_HOOK
47-
/**
48-
* @brief Register file read/write access event callback function.
49-
*
50-
* @param cb Callback function or NULL to disable.
51-
*/
52-
void fs_mgmt_register_evt_cb(fs_mgmt_on_evt_cb cb);
53-
#endif
54-
5529
#ifdef __cplusplus
5630
}
5731
#endif

subsys/mgmt/mcumgr/lib/cmd/fs_mgmt/src/fs_mgmt.c

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#include "fs_mgmt/hash_checksum_sha256.h"
3333
#endif
3434

35+
#if defined(CONFIG_MCUMGR_MGMT_NOTIFICATION_HOOKS)
36+
#include <zephyr/mgmt/mcumgr/mgmt/callbacks.h>
37+
#endif
38+
3539
#ifdef CONFIG_FS_MGMT_CHECKSUM_HASH
3640
/* Define default hash/checksum */
3741
#if defined(CONFIG_FS_MGMT_CHECKSUM_IEEE_CRC32)
@@ -79,10 +83,6 @@ struct hash_checksum_iterator_info {
7983
};
8084
#endif
8185

82-
#ifdef CONFIG_FS_MGMT_FILE_ACCESS_HOOK
83-
static fs_mgmt_on_evt_cb fs_evt_cb;
84-
#endif
85-
8686
static int fs_mgmt_filelen(const char *path, size_t *out_len)
8787
{
8888
struct fs_dirent dirent;
@@ -179,6 +179,13 @@ static int fs_mgmt_file_download(struct smp_streamer *ctxt)
179179
ZCBOR_MAP_DECODE_KEY_VAL(name, zcbor_tstr_decode, &name),
180180
};
181181

182+
#if defined(CONFIG_MCUMGR_GRP_FS_FILE_ACCESS_HOOK)
183+
struct fs_mgmt_file_access file_access_data = {
184+
.upload = false,
185+
.filename = path,
186+
};
187+
#endif
188+
182189
ok = zcbor_map_decode_bulk(zsd, fs_download_decode,
183190
ARRAY_SIZE(fs_download_decode), &decoded) == 0;
184191

@@ -189,14 +196,13 @@ static int fs_mgmt_file_download(struct smp_streamer *ctxt)
189196
memcpy(path, name.value, name.len);
190197
path[name.len] = '\0';
191198

192-
#ifdef CONFIG_FS_MGMT_FILE_ACCESS_HOOK
193-
if (fs_evt_cb != NULL) {
194-
/* Send request to application to check if access should be allowed or not */
195-
rc = fs_evt_cb(false, path);
199+
#if defined(CONFIG_MCUMGR_GRP_FS_FILE_ACCESS_HOOK)
200+
/* Send request to application to check if access should be allowed or not */
201+
rc = mgmt_callback_notify(MGMT_EVT_OP_FS_MGMT_FILE_ACCESS, &file_access_data,
202+
sizeof(file_access_data));
196203

197-
if (rc != 0) {
198-
return rc;
199-
}
204+
if (rc != MGMT_ERR_EOK) {
205+
return rc;
200206
}
201207
#endif
202208

@@ -313,6 +319,13 @@ static int fs_mgmt_file_upload(struct smp_streamer *ctxt)
313319
ZCBOR_MAP_DECODE_KEY_VAL(len, zcbor_uint64_decode, &len),
314320
};
315321

322+
#if defined(CONFIG_MCUMGR_GRP_FS_FILE_ACCESS_HOOK)
323+
struct fs_mgmt_file_access file_access_data = {
324+
.upload = false,
325+
.filename = file_name,
326+
};
327+
#endif
328+
316329
ok = zcbor_map_decode_bulk(zsd, fs_upload_decode,
317330
ARRAY_SIZE(fs_upload_decode), &decoded) == 0;
318331

@@ -324,14 +337,13 @@ static int fs_mgmt_file_upload(struct smp_streamer *ctxt)
324337
memcpy(file_name, name.value, name.len);
325338
file_name[name.len] = '\0';
326339

327-
#ifdef CONFIG_FS_MGMT_FILE_ACCESS_HOOK
328-
if (fs_evt_cb != NULL) {
329-
/* Send request to application to check if access should be allowed or not */
330-
rc = fs_evt_cb(true, file_name);
340+
#if defined(CONFIG_MCUMGR_GRP_FS_FILE_ACCESS_HOOK)
341+
/* Send request to application to check if access should be allowed or not */
342+
rc = mgmt_callback_notify(MGMT_EVT_OP_FS_MGMT_FILE_ACCESS, &file_access_data,
343+
sizeof(file_access_data));
331344

332-
if (rc != 0) {
333-
return rc;
334-
}
345+
if (rc != MGMT_ERR_EOK) {
346+
return rc;
335347
}
336348
#endif
337349

@@ -672,10 +684,3 @@ void fs_mgmt_register_group(void)
672684
#endif
673685
#endif
674686
}
675-
676-
#ifdef CONFIG_FS_MGMT_FILE_ACCESS_HOOK
677-
void fs_mgmt_register_evt_cb(fs_mgmt_on_evt_cb cb)
678-
{
679-
fs_evt_cb = cb;
680-
}
681-
#endif

0 commit comments

Comments
 (0)