Skip to content

Commit f869e11

Browse files
committed
[nrf fromtree] mgmt: mcumgr: grp: img_mgmt: Add optional image slot state callback
Adds an optional callback which can be used to append custom fields to the image slot state command response Signed-off-by: Jamie McCrae <[email protected]> (cherry picked from commit 031f9f2)
1 parent 672eda8 commit f869e11

File tree

4 files changed

+80
-22
lines changed

4 files changed

+80
-22
lines changed

include/zephyr/mgmt/mcumgr/grp/img_mgmt/img_mgmt_callbacks.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#ifndef H_MCUMGR_IMG_MGMT_CALLBACKS_
99
#define H_MCUMGR_IMG_MGMT_CALLBACKS_
1010

11+
#include <stdbool.h>
12+
#include <stdint.h>
13+
#include <zcbor_common.h>
14+
1115
#ifdef __cplusplus
1216
extern "C" {
1317
#endif
@@ -37,6 +41,20 @@ struct img_mgmt_upload_check {
3741
struct img_mgmt_upload_req *req;
3842
};
3943

44+
/**
45+
* Structure provided in the #MGMT_EVT_OP_IMG_MGMT_IMAGE_SLOT_STATE notification callback: This
46+
* callback function is used to allow applications or modules append custom fields to the image
47+
* slot state response.
48+
*/
49+
struct img_mgmt_state_slot_encode {
50+
bool *ok;
51+
zcbor_state_t *zse;
52+
const uint32_t slot;
53+
const char *version;
54+
const uint8_t *hash;
55+
const int flags;
56+
};
57+
4058
/**
4159
* @}
4260
*/

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ enum img_mgmt_group_events {
180180
/** Callback when an image write command has finished writing to flash. */
181181
MGMT_EVT_OP_IMG_MGMT_DFU_CHUNK_WRITE_COMPLETE = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_IMG, 5),
182182

183+
/** Callback when an image slot's state is encoded for a response. */
184+
MGMT_EVT_OP_IMG_MGMT_IMAGE_SLOT_STATE = MGMT_DEF_EVT_OP_ID(MGMT_EVT_GRP_IMG, 6),
185+
183186
/** Used to enable all img_mgmt_group events. */
184187
MGMT_EVT_OP_IMG_MGMT_ALL = MGMT_DEF_EVT_OP_ALL(MGMT_EVT_GRP_IMG),
185188
};

subsys/mgmt/mcumgr/grp/img_mgmt/Kconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,22 @@ config MCUMGR_GRP_IMG_TOO_LARGE_BOOTLOADER_INFO
185185

186186
endchoice
187187

188+
config MCUMGR_GRP_IMG_IMAGE_SLOT_STATE_HOOK
189+
bool "Image slot state hook"
190+
depends on MCUMGR_MGMT_NOTIFICATION_HOOKS
191+
help
192+
Allows applications to add additional fields to responses for the image slot state
193+
command.
194+
195+
config MCUMGR_GRP_IMG_IMAGE_SLOT_STATE_STATES
196+
int
197+
prompt "Predicted maximum number of entries per group" if MCUMGR_GRP_IMG_IMAGE_SLOT_STATE_HOOK
198+
default 15
199+
help
200+
This is used for defining CBOR map holding group data.
201+
The value does not affect memory allocation, it is used by zcbor
202+
to figure out how to encode map depending on its predicted size.
203+
188204
config MCUMGR_GRP_IMG_QSPI_XIP_SPLIT_IMAGE
189205
bool "QSPI XIP Split image mode"
190206
depends on MCUBOOT_BOOTLOADER_MODE_DIRECT_XIP

subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt_state.c

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,11 @@
2828
#include <zephyr/mgmt/mcumgr/mgmt/callbacks.h>
2929
#endif
3030

31-
LOG_MODULE_DECLARE(mcumgr_img_grp, CONFIG_MCUMGR_GRP_IMG_LOG_LEVEL);
31+
#ifdef CONFIG_MCUMGR_GRP_IMG_IMAGE_SLOT_STATE_HOOK
32+
#include <mgmt/mcumgr/transport/smp_internal.h>
33+
#endif
3234

33-
/* The value here sets how many "characteristics" that describe image is
34-
* encoded into a map per each image (like bootable flags, and so on).
35-
* This value is only used for zcbor to predict map size and map encoding
36-
* and does not affect memory allocation.
37-
* In case when more "characteristics" are added to image map then
38-
* zcbor_map_end_encode may fail it this value does not get updated.
39-
*/
40-
#define MAX_IMG_CHARACTERISTICS 15
35+
LOG_MODULE_DECLARE(mcumgr_img_grp, CONFIG_MCUMGR_GRP_IMG_LOG_LEVEL);
4136

4237
#ifndef CONFIG_MCUMGR_GRP_IMG_FRUGAL_LIST
4338
#define ZCBOR_ENCODE_FLAG(zse, label, value) \
@@ -415,8 +410,9 @@ img_mgmt_state_confirm(void)
415410
}
416411

417412
/* Return zcbor encoding result */
418-
static bool img_mgmt_state_encode_slot(zcbor_state_t *zse, uint32_t slot, int state_flags)
413+
static bool img_mgmt_state_encode_slot(struct smp_streamer *ctxt, uint32_t slot, int state_flags)
419414
{
415+
zcbor_state_t *zse = ctxt->writer->zs;
420416
uint32_t flags;
421417
char vers_str[IMG_MGMT_VER_MAX_STR_LEN];
422418
uint8_t hash[IMAGE_HASH_LEN]; /* SHA256 hash */
@@ -425,17 +421,30 @@ static bool img_mgmt_state_encode_slot(zcbor_state_t *zse, uint32_t slot, int st
425421
bool ok;
426422
int rc = img_mgmt_read_info(slot, &ver, hash, &flags);
427423

424+
#if defined(CONFIG_MCUMGR_GRP_IMG_IMAGE_SLOT_STATE_HOOK)
425+
int32_t err_rc;
426+
uint16_t err_group;
427+
struct img_mgmt_state_slot_encode slot_encode_data = {
428+
.ok = &ok,
429+
.zse = zse,
430+
.slot = slot,
431+
.version = vers_str,
432+
.hash = hash,
433+
.flags = flags,
434+
};
435+
#endif
436+
428437
if (rc != 0) {
429438
/* zcbor encoding did not fail */
430439
return true;
431440
}
432441

433-
ok = zcbor_map_start_encode(zse, MAX_IMG_CHARACTERISTICS) &&
442+
ok = zcbor_map_start_encode(zse, CONFIG_MCUMGR_GRP_IMG_IMAGE_SLOT_STATE_STATES) &&
434443
(CONFIG_MCUMGR_GRP_IMG_UPDATABLE_IMAGE_NUMBER == 1 ||
435-
(zcbor_tstr_put_lit(zse, "image") &&
436-
zcbor_uint32_put(zse, slot >> 1))) &&
437-
zcbor_tstr_put_lit(zse, "slot") &&
438-
zcbor_uint32_put(zse, slot % 2) &&
444+
(zcbor_tstr_put_lit(zse, "image") &&
445+
zcbor_uint32_put(zse, slot >> 1))) &&
446+
zcbor_tstr_put_lit(zse, "slot") &&
447+
zcbor_uint32_put(zse, slot % 2) &&
439448
zcbor_tstr_put_lit(zse, "version");
440449

441450
if (ok) {
@@ -447,15 +456,27 @@ static bool img_mgmt_state_encode_slot(zcbor_state_t *zse, uint32_t slot, int st
447456
}
448457
}
449458

450-
ok = ok && zcbor_tstr_put_lit(zse, "hash") &&
459+
ok = ok && zcbor_tstr_put_lit(zse, "hash") &&
451460
zcbor_bstr_encode(zse, &zhash) &&
452461
ZCBOR_ENCODE_FLAG(zse, "bootable", !(flags & IMAGE_F_NON_BOOTABLE)) &&
453462
ZCBOR_ENCODE_FLAG(zse, "pending", state_flags & REPORT_SLOT_PENDING) &&
454463
ZCBOR_ENCODE_FLAG(zse, "confirmed", state_flags & REPORT_SLOT_CONFIRMED) &&
455464
ZCBOR_ENCODE_FLAG(zse, "active", state_flags & REPORT_SLOT_ACTIVE) &&
456-
ZCBOR_ENCODE_FLAG(zse, "permanent", state_flags & REPORT_SLOT_PERMANENT) &&
457-
zcbor_map_end_encode(zse, MAX_IMG_CHARACTERISTICS);
465+
ZCBOR_ENCODE_FLAG(zse, "permanent", state_flags & REPORT_SLOT_PERMANENT);
466+
467+
if (!ok) {
468+
goto failed;
469+
}
470+
471+
#if defined(CONFIG_MCUMGR_GRP_IMG_IMAGE_SLOT_STATE_HOOK)
472+
/* Send notification to application to optionally append more fields */
473+
(void)mgmt_callback_notify(MGMT_EVT_OP_IMG_MGMT_IMAGE_SLOT_STATE, &slot_encode_data,
474+
sizeof(slot_encode_data), &err_rc, &err_group);
475+
#endif
476+
477+
ok &= zcbor_map_end_encode(zse, CONFIG_MCUMGR_GRP_IMG_IMAGE_SLOT_STATE_STATES);
458478

479+
failed:
459480
return ok;
460481
}
461482

@@ -499,11 +520,11 @@ img_mgmt_state_read(struct smp_streamer *ctxt)
499520

500521
/* Need to report slots in proper order */
501522
if (slot_a < slot_o) {
502-
ok = img_mgmt_state_encode_slot(zse, slot_a, flags_a) &&
503-
img_mgmt_state_encode_slot(zse, slot_o, flags_o);
523+
ok = img_mgmt_state_encode_slot(ctxt, slot_a, flags_a) &&
524+
img_mgmt_state_encode_slot(ctxt, slot_o, flags_o);
504525
} else {
505-
ok = img_mgmt_state_encode_slot(zse, slot_o, flags_o) &&
506-
img_mgmt_state_encode_slot(zse, slot_a, flags_a);
526+
ok = img_mgmt_state_encode_slot(ctxt, slot_o, flags_o) &&
527+
img_mgmt_state_encode_slot(ctxt, slot_a, flags_a);
507528
}
508529
}
509530

0 commit comments

Comments
 (0)