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