Skip to content

Commit

Permalink
mgmt: mcumgr: Make SMP version 2 to legacy error lookup extensible
Browse files Browse the repository at this point in the history
Replaces the manual lookup function with a lookup function which
is provided when registering MCUmgr handlers which can be used to
find the function to translate error codes, allowing out of tree
MCUmgr handlers to provide error translation handlers.

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
  • Loading branch information
nordicjm authored and fabiobaltieri committed Jul 20, 2023
1 parent f603061 commit ecfbabd
Show file tree
Hide file tree
Showing 16 changed files with 279 additions and 264 deletions.
11 changes: 0 additions & 11 deletions include/zephyr/mgmt/mcumgr/grp/fs_mgmt/fs_mgmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,6 @@ enum fs_mgmt_ret_code_t {
FS_MGMT_RET_RC_CHECKSUM_HASH_NOT_FOUND,
};

#ifdef CONFIG_MCUMGR_SMP_SUPPORT_ORIGINAL_PROTOCOL
/*
* @brief Translate FS mgmt group error code into MCUmgr error code
*
* @param ret #fs_mgmt_ret_code_t error code
*
* @return #mcumgr_err_t error code
*/
int fs_mgmt_translate_error_code(uint16_t ret);
#endif

#ifdef __cplusplus
}
#endif
Expand Down
11 changes: 0 additions & 11 deletions include/zephyr/mgmt/mcumgr/grp/img_mgmt/img_mgmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,17 +341,6 @@ int img_mgmt_vercmp(const struct image_version *a, const struct image_version *b
void img_mgmt_reset_upload(void);
#endif

#ifdef CONFIG_MCUMGR_SMP_SUPPORT_ORIGINAL_PROTOCOL
/*
* @brief Translate IMG mgmt group error code into MCUmgr error code
*
* @param ret #img_mgmt_ret_code_t error code
*
* @return #mcumgr_err_t error code
*/
int img_mgmt_translate_error_code(uint16_t ret);
#endif

#ifdef CONFIG_MCUMGR_GRP_IMG_VERBOSE_ERR
#define IMG_MGMT_UPLOAD_ACTION_SET_RC_RSN(action, rsn) ((action)->rc_rsn = (rsn))
#define IMG_MGMT_UPLOAD_ACTION_RC_RSN(action) ((action)->rc_rsn)
Expand Down
12 changes: 1 addition & 11 deletions include/zephyr/mgmt/mcumgr/grp/os_mgmt/os_mgmt.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2018-2021 mcumgr authors
* Copyright (c) 2022 Laird Connectivity
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -97,17 +98,6 @@ struct os_mgmt_info_append {
bool *prior_output;
};

#ifdef CONFIG_MCUMGR_SMP_SUPPORT_ORIGINAL_PROTOCOL
/*
* @brief Translate OS mgmt group error code into MCUmgr error code
*
* @param ret #os_mgmt_ret_code_t error code
*
* @return #mcumgr_err_t error code
*/
int os_mgmt_translate_error_code(uint16_t ret);
#endif

#ifdef __cplusplus
}
#endif
Expand Down
11 changes: 0 additions & 11 deletions include/zephyr/mgmt/mcumgr/grp/shell_mgmt/shell_mgmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,6 @@ enum shell_mgmt_ret_code_t {
SHELL_MGMT_RET_RC_EMPTY_COMMAND,
};

#ifdef CONFIG_MCUMGR_SMP_SUPPORT_ORIGINAL_PROTOCOL
/*
* @brief Translate shell mgmt group error code into MCUmgr error code
*
* @param ret #shell_mgmt_ret_code_t error code
*
* @return #mcumgr_err_t error code
*/
int shell_mgmt_translate_error_code(uint16_t ret);
#endif

#ifdef __cplusplus
}
#endif
Expand Down
11 changes: 0 additions & 11 deletions include/zephyr/mgmt/mcumgr/grp/stat_mgmt/stat_mgmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,6 @@ struct stat_mgmt_entry {
uint64_t value;
};

#ifdef CONFIG_MCUMGR_SMP_SUPPORT_ORIGINAL_PROTOCOL
/*
* @brief Translate stat mgmt group error code into MCUmgr error code
*
* @param ret #stat_mgmt_ret_code_t error code
*
* @return #mcumgr_err_t error code
*/
int stat_mgmt_translate_error_code(uint16_t ret);
#endif

#ifdef __cplusplus
}
#endif
Expand Down
11 changes: 0 additions & 11 deletions include/zephyr/mgmt/mcumgr/grp/zephyr/zephyr_basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,6 @@ enum zephyr_basic_group_ret_code_t {
ZEPHYR_MGMT_GRP_CMD_RC_FLASH_ERASE_FAILED,
};

#ifdef CONFIG_MCUMGR_SMP_SUPPORT_ORIGINAL_PROTOCOL
/*
* @brief Translate zephyr basic group error code into MCUmgr error code
*
* @param ret #zephyr_basic_group_ret_code_t error code
*
* @return #mcumgr_err_t error code
*/
int zephyr_basic_group_translate_error_code(uint16_t ret);
#endif

#ifdef __cplusplus
}
#endif
Expand Down
24 changes: 22 additions & 2 deletions include/zephyr/mgmt/mcumgr/mgmt/mgmt.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2018-2021 mcumgr authors
* Copyright (c) 2022 Nordic Semiconductor ASA
* Copyright (c) 2022-2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -195,8 +195,15 @@ struct mgmt_group {
const struct mgmt_handler *mg_handlers;
uint16_t mg_handlers_count;

/* The numeric ID of this group. */
/** The numeric ID of this group. */
uint16_t mg_group_id;

#if IS_ENABLED(CONFIG_MCUMGR_SMP_SUPPORT_ORIGINAL_PROTOCOL)
/** A function handler for translating version 2 SMP error codes to version 1 SMP error
* codes (optional)
*/
smp_translate_error_fn mg_translate_error;
#endif
};

/**
Expand Down Expand Up @@ -224,6 +231,19 @@ void mgmt_unregister_group(struct mgmt_group *group);
*/
const struct mgmt_handler *mgmt_find_handler(uint16_t group_id, uint16_t command_id);

#if IS_ENABLED(CONFIG_MCUMGR_SMP_SUPPORT_ORIGINAL_PROTOCOL)
/**
* @brief Finds a registered error translation function for converting from SMP
* version 2 error codes to legacy SMP version 1 error codes.
*
* @param group_id The group of the translation function to find.
*
* @return Requested lookup function on success.
* @return NULL on failure.
*/
smp_translate_error_fn mgmt_find_error_translation_function(uint16_t group_id);
#endif

/**
* @}
*/
Expand Down
14 changes: 13 additions & 1 deletion include/zephyr/mgmt/mcumgr/smp/smp.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2021 mcumgr authors
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -55,7 +56,7 @@ struct cbor_nb_writer {
struct net_buf *nb;
zcbor_state_t zs[2];

#ifdef CONFIG_MCUMGR_SMP_SUPPORT_ORIGINAL_PROTOCOL
#if IS_ENABLED(CONFIG_MCUMGR_SMP_SUPPORT_ORIGINAL_PROTOCOL)
uint16_t error_group;
uint16_t error_ret;
#endif
Expand Down Expand Up @@ -120,6 +121,17 @@ int smp_process_request_packet(struct smp_streamer *streamer, void *req);
*/
bool smp_add_cmd_ret(zcbor_state_t *zse, uint16_t group, uint16_t ret);

#if IS_ENABLED(CONFIG_MCUMGR_SMP_SUPPORT_ORIGINAL_PROTOCOL)
/** @typedef smp_translate_error_fn
* @brief Translates a SMP version 2 error response to a legacy SMP version 1 error code.
*
* @param ret The SMP version 2 error ret/rc value.
*
* @return #enum mcumgr_err_t Legacy SMP version 1 error code to return to client.
*/
typedef int (*smp_translate_error_fn)(uint16_t ret);
#endif

#ifdef __cplusplus
}
#endif
Expand Down
76 changes: 43 additions & 33 deletions subsys/mgmt/mcumgr/grp/fs_mgmt/src/fs_mgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,46 @@ static int fs_mgmt_close_opened_file(struct smp_streamer *ctxt)
return MGMT_ERR_EOK;
}

#ifdef CONFIG_MCUMGR_SMP_SUPPORT_ORIGINAL_PROTOCOL
/*
* @brief Translate FS mgmt group error code into MCUmgr error code
*
* @param ret #fs_mgmt_ret_code_t error code
*
* @return #mcumgr_err_t error code
*/
static int fs_mgmt_translate_error_code(uint16_t ret)
{
int rc;

switch (ret) {
case FS_MGMT_RET_RC_FILE_INVALID_NAME:
case FS_MGMT_RET_RC_CHECKSUM_HASH_NOT_FOUND:
rc = MGMT_ERR_EINVAL;
break;

case FS_MGMT_RET_RC_FILE_NOT_FOUND:
case FS_MGMT_RET_RC_FILE_IS_DIRECTORY:
rc = MGMT_ERR_ENOENT;
break;

case FS_MGMT_RET_RC_UNKNOWN:
case FS_MGMT_RET_RC_FILE_OPEN_FAILED:
case FS_MGMT_RET_RC_FILE_SEEK_FAILED:
case FS_MGMT_RET_RC_FILE_READ_FAILED:
case FS_MGMT_RET_RC_FILE_TRUNCATE_FAILED:
case FS_MGMT_RET_RC_FILE_DELETE_FAILED:
case FS_MGMT_RET_RC_FILE_WRITE_FAILED:
case FS_MGMT_RET_RC_FILE_OFFSET_NOT_VALID:
case FS_MGMT_RET_RC_FILE_OFFSET_LARGER_THAN_FILE:
default:
rc = MGMT_ERR_EUNKNOWN;
}

return rc;
}
#endif

static const struct mgmt_handler fs_mgmt_handlers[] = {
[FS_MGMT_ID_FILE] = {
.mh_read = fs_mgmt_file_download,
Expand Down Expand Up @@ -931,6 +971,9 @@ static struct mgmt_group fs_mgmt_group = {
.mg_handlers = fs_mgmt_handlers,
.mg_handlers_count = FS_MGMT_HANDLER_CNT,
.mg_group_id = MGMT_GROUP_ID_FS,
#ifdef CONFIG_MCUMGR_SMP_SUPPORT_ORIGINAL_PROTOCOL
.mg_translate_error = fs_mgmt_translate_error_code,
#endif
};

static void fs_mgmt_register_group(void)
Expand All @@ -954,37 +997,4 @@ static void fs_mgmt_register_group(void)
#endif
}

#ifdef CONFIG_MCUMGR_SMP_SUPPORT_ORIGINAL_PROTOCOL
int fs_mgmt_translate_error_code(uint16_t ret)
{
int rc;

switch (ret) {
case FS_MGMT_RET_RC_FILE_INVALID_NAME:
case FS_MGMT_RET_RC_CHECKSUM_HASH_NOT_FOUND:
rc = MGMT_ERR_EINVAL;
break;

case FS_MGMT_RET_RC_FILE_NOT_FOUND:
case FS_MGMT_RET_RC_FILE_IS_DIRECTORY:
rc = MGMT_ERR_ENOENT;
break;

case FS_MGMT_RET_RC_UNKNOWN:
case FS_MGMT_RET_RC_FILE_OPEN_FAILED:
case FS_MGMT_RET_RC_FILE_SEEK_FAILED:
case FS_MGMT_RET_RC_FILE_READ_FAILED:
case FS_MGMT_RET_RC_FILE_TRUNCATE_FAILED:
case FS_MGMT_RET_RC_FILE_DELETE_FAILED:
case FS_MGMT_RET_RC_FILE_WRITE_FAILED:
case FS_MGMT_RET_RC_FILE_OFFSET_NOT_VALID:
case FS_MGMT_RET_RC_FILE_OFFSET_LARGER_THAN_FILE:
default:
rc = MGMT_ERR_EUNKNOWN;
}

return rc;
}
#endif

MCUMGR_HANDLER_DEFINE(fs_mgmt, fs_mgmt_register_group);
80 changes: 45 additions & 35 deletions subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -752,42 +752,15 @@ int img_mgmt_my_version(struct image_version *ver)
ver, NULL, NULL);
}

static const struct mgmt_handler img_mgmt_handlers[] = {
[IMG_MGMT_ID_STATE] = {
.mh_read = img_mgmt_state_read,
#ifdef CONFIG_MCUBOOT_BOOTLOADER_MODE_DIRECT_XIP
.mh_write = NULL
#else
.mh_write = img_mgmt_state_write,
#endif
},
[IMG_MGMT_ID_UPLOAD] = {
.mh_read = NULL,
.mh_write = img_mgmt_upload
},
[IMG_MGMT_ID_ERASE] = {
.mh_read = NULL,
.mh_write = img_mgmt_erase
},
};

static const struct mgmt_handler img_mgmt_handlers[];

#define IMG_MGMT_HANDLER_CNT ARRAY_SIZE(img_mgmt_handlers)

static struct mgmt_group img_mgmt_group = {
.mg_handlers = (struct mgmt_handler *)img_mgmt_handlers,
.mg_handlers_count = IMG_MGMT_HANDLER_CNT,
.mg_group_id = MGMT_GROUP_ID_IMAGE,
};

static void img_mgmt_register_group(void)
{
mgmt_register_group(&img_mgmt_group);
}

#ifdef CONFIG_MCUMGR_SMP_SUPPORT_ORIGINAL_PROTOCOL
int img_mgmt_translate_error_code(uint16_t ret)
/*
* @brief Translate IMG mgmt group error code into MCUmgr error code
*
* @param ret #img_mgmt_ret_code_t error code
*
* @return #mcumgr_err_t error code
*/
static int img_mgmt_translate_error_code(uint16_t ret)
{
int rc;

Expand Down Expand Up @@ -841,4 +814,41 @@ int img_mgmt_translate_error_code(uint16_t ret)
}
#endif

static const struct mgmt_handler img_mgmt_handlers[] = {
[IMG_MGMT_ID_STATE] = {
.mh_read = img_mgmt_state_read,
#ifdef CONFIG_MCUBOOT_BOOTLOADER_MODE_DIRECT_XIP
.mh_write = NULL
#else
.mh_write = img_mgmt_state_write,
#endif
},
[IMG_MGMT_ID_UPLOAD] = {
.mh_read = NULL,
.mh_write = img_mgmt_upload
},
[IMG_MGMT_ID_ERASE] = {
.mh_read = NULL,
.mh_write = img_mgmt_erase
},
};

static const struct mgmt_handler img_mgmt_handlers[];

#define IMG_MGMT_HANDLER_CNT ARRAY_SIZE(img_mgmt_handlers)

static struct mgmt_group img_mgmt_group = {
.mg_handlers = (struct mgmt_handler *)img_mgmt_handlers,
.mg_handlers_count = IMG_MGMT_HANDLER_CNT,
.mg_group_id = MGMT_GROUP_ID_IMAGE,
#ifdef CONFIG_MCUMGR_SMP_SUPPORT_ORIGINAL_PROTOCOL
.mg_translate_error = img_mgmt_translate_error_code,
#endif
};

static void img_mgmt_register_group(void)
{
mgmt_register_group(&img_mgmt_group);
}

MCUMGR_HANDLER_DEFINE(img_mgmt, img_mgmt_register_group);
Loading

0 comments on commit ecfbabd

Please sign in to comment.