diff --git a/subsys/mgmt/mcumgr/Kconfig b/subsys/mgmt/mcumgr/Kconfig index ad58280d104..c42a6aa07c2 100644 --- a/subsys/mgmt/mcumgr/Kconfig +++ b/subsys/mgmt/mcumgr/Kconfig @@ -118,6 +118,18 @@ config MCUMGR_SMP_COMMAND_STATUS_HOOKS This will enable SMP command status notification hooks for when an SMP message is received or processed. +config MCUMGR_SMP_LEGACY_RC_BEHAVIOUR + bool "Legacy rc (result code) response behaviour" + help + This will enable legacy result code response behaviour of having rc + present in responses when the status is 0. With this option disabled, + mcumgr acts with new behaviour and will only return rc is the result + code is non-zero (i.e. an error occurred). + + If a command only returns a result code, this will mean that the + response will be empty with the new behaviour enabled, as opposed to + the old behaviour where the rc field will be present in the response. + menu "Command Handlers" rsource "grp/Kconfig" diff --git a/subsys/mgmt/mcumgr/grp/fs_mgmt/src/fs_mgmt.c b/subsys/mgmt/mcumgr/grp/fs_mgmt/src/fs_mgmt.c index 9e4479d53b5..187c168c965 100644 --- a/subsys/mgmt/mcumgr/grp/fs_mgmt/src/fs_mgmt.c +++ b/subsys/mgmt/mcumgr/grp/fs_mgmt/src/fs_mgmt.c @@ -112,14 +112,15 @@ static int fs_mgmt_filelen(const char *path, size_t *out_len) */ static bool fs_mgmt_file_rsp(zcbor_state_t *zse, int rc, uint64_t off) { - bool ok; + bool ok = true; - ok = zcbor_tstr_put_lit(zse, "rc") && - zcbor_int32_put(zse, rc) && - zcbor_tstr_put_lit(zse, "off") && - zcbor_uint64_put(zse, off); + if (IS_ENABLED(CONFIG_MCUMGR_SMP_LEGACY_RC_BEHAVIOUR) || rc != 0) { + ok = zcbor_tstr_put_lit(zse, "rc") && + zcbor_int32_put(zse, rc); + } - return ok; + return ok && zcbor_tstr_put_lit(zse, "off") && + zcbor_uint64_put(zse, off); } static int fs_mgmt_read(const char *path, size_t offset, size_t len, @@ -425,17 +426,19 @@ static int fs_mgmt_file_status(struct smp_streamer *ctxt) /* Retrieve file size */ rc = fs_mgmt_filelen(path, &file_len); + if (rc != 0) { + return rc; + } + /* Encode the response. */ - if (rc == 0) { - /* No error, only encode file status (length) */ - ok = zcbor_tstr_put_lit(zse, "len") && - zcbor_uint64_put(zse, file_len); - } else { - /* Error, only encode error result code */ + if (IS_ENABLED(CONFIG_MCUMGR_SMP_LEGACY_RC_BEHAVIOUR)) { ok = zcbor_tstr_put_lit(zse, "rc") && zcbor_int32_put(zse, rc); } + ok = ok && zcbor_tstr_put_lit(zse, "len") && + zcbor_uint64_put(zse, file_len); + if (!ok) { return MGMT_ERR_EMSGSIZE; } @@ -535,51 +538,50 @@ static int fs_mgmt_file_hash_checksum(struct smp_streamer *ctxt) /* Encode the response */ if (rc != 0) { - ok = zcbor_tstr_put_lit(zse, "rc") && - zcbor_int32_put(zse, rc); + return rc; + } + + ok &= zcbor_tstr_put_lit(zse, "type") && + zcbor_tstr_put_term(zse, type_arr); + + if (off != 0) { + ok &= zcbor_tstr_put_lit(zse, "off") && + zcbor_uint64_put(zse, off); + } + + ok &= zcbor_tstr_put_lit(zse, "len") && + zcbor_uint64_put(zse, file_len) && + zcbor_tstr_put_lit(zse, "output"); + + if (group->byte_string == true) { + /* Output is a byte string */ + ok &= zcbor_bstr_encode_ptr(zse, output, group->output_size); } else { - ok = zcbor_tstr_put_lit(zse, "type") && - zcbor_tstr_put_term(zse, type_arr); + /* Output is a number */ + uint64_t tmp_val = 0; - if (off != 0) { - ok &= zcbor_tstr_put_lit(zse, "off") && - zcbor_uint64_put(zse, off); - } - - ok &= zcbor_tstr_put_lit(zse, "len") && - zcbor_uint64_put(zse, file_len) && - zcbor_tstr_put_lit(zse, "output"); - - if (group->byte_string == true) { - /* Output is a byte string */ - ok &= zcbor_bstr_encode_ptr(zse, output, group->output_size); - } else { - /* Output is a number */ - uint64_t tmp_val = 0; - - if (group->output_size == sizeof(uint8_t)) { - tmp_val = (uint64_t)(*(uint8_t *)output); + if (group->output_size == sizeof(uint8_t)) { + tmp_val = (uint64_t)(*(uint8_t *)output); #if FS_MGMT_CHECKSUM_HASH_LARGEST_OUTPUT_SIZE > 1 - } else if (group->output_size == sizeof(uint16_t)) { - tmp_val = (uint64_t)(*(uint16_t *)output); + } else if (group->output_size == sizeof(uint16_t)) { + tmp_val = (uint64_t)(*(uint16_t *)output); #if FS_MGMT_CHECKSUM_HASH_LARGEST_OUTPUT_SIZE > 2 - } else if (group->output_size == sizeof(uint32_t)) { - tmp_val = (uint64_t)(*(uint32_t *)output); + } else if (group->output_size == sizeof(uint32_t)) { + tmp_val = (uint64_t)(*(uint32_t *)output); #if FS_MGMT_CHECKSUM_HASH_LARGEST_OUTPUT_SIZE > 4 - } else if (group->output_size == sizeof(uint64_t)) { - tmp_val = (*(uint64_t *)output); + } else if (group->output_size == sizeof(uint64_t)) { + tmp_val = (*(uint64_t *)output); #endif #endif #endif - } else { - LOG_ERR("Unable to handle numerical checksum size %u", - group->output_size); + } else { + LOG_ERR("Unable to handle numerical checksum size %u", + group->output_size); - return MGMT_ERR_EUNKNOWN; - } - - ok &= zcbor_uint64_put(zse, tmp_val); + return MGMT_ERR_EUNKNOWN; } + + ok &= zcbor_uint64_put(zse, tmp_val); } if (!ok) { diff --git a/subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt.c b/subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt.c index 9db13d20415..fff46b085dd 100644 --- a/subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt.c +++ b/subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt.c @@ -303,7 +303,8 @@ img_mgmt_erase(struct smp_streamer *ctxt) return rc; } - if (zcbor_tstr_put_lit(zse, "rc") && zcbor_int32_put(zse, 0)) { + if (IS_ENABLED(CONFIG_MCUMGR_SMP_LEGACY_RC_BEHAVIOUR) && zcbor_tstr_put_lit(zse, "rc") && + zcbor_int32_put(zse, 0)) { return MGMT_ERR_EOK; } @@ -314,12 +315,15 @@ static int img_mgmt_upload_good_rsp(struct smp_streamer *ctxt) { zcbor_state_t *zse = ctxt->writer->zs; - bool ok; + bool ok = true; - ok = zcbor_tstr_put_lit(zse, "rc") && - zcbor_int32_put(zse, MGMT_ERR_EOK) && - zcbor_tstr_put_lit(zse, "off") && - zcbor_size_put(zse, g_img_mgmt_state.off); + if (IS_ENABLED(CONFIG_MCUMGR_SMP_LEGACY_RC_BEHAVIOUR)) { + ok = zcbor_tstr_put_lit(zse, "rc") && + zcbor_int32_put(zse, MGMT_ERR_EOK); + } + + ok = ok && zcbor_tstr_put_lit(zse, "off") && + zcbor_size_put(zse, g_img_mgmt_state.off); return ok ? MGMT_ERR_EOK : MGMT_ERR_EMSGSIZE; } diff --git a/subsys/mgmt/mcumgr/grp/stat_mgmt/src/stat_mgmt.c b/subsys/mgmt/mcumgr/grp/stat_mgmt/src/stat_mgmt.c index 2f9f42d25f4..917ec47b648 100644 --- a/subsys/mgmt/mcumgr/grp/stat_mgmt/src/stat_mgmt.c +++ b/subsys/mgmt/mcumgr/grp/stat_mgmt/src/stat_mgmt.c @@ -155,12 +155,17 @@ stat_mgmt_show(struct smp_streamer *ctxt) return MGMT_ERR_EUNKNOWN; } - ok = zcbor_tstr_put_lit(zse, "rc") && - zcbor_int32_put(zse, MGMT_ERR_EOK) && - zcbor_tstr_put_lit(zse, "name") && - zcbor_tstr_encode(zse, &value) && - zcbor_tstr_put_lit(zse, "fields") && - zcbor_map_start_encode(zse, counter); + if (IS_ENABLED(CONFIG_MCUMGR_SMP_LEGACY_RC_BEHAVIOUR)) { + ok = zcbor_tstr_put_lit(zse, "rc") && + zcbor_int32_put(zse, MGMT_ERR_EOK); + } + + if (ok) { + ok = zcbor_tstr_put_lit(zse, "name") && + zcbor_tstr_encode(zse, &value) && + zcbor_tstr_put_lit(zse, "fields") && + zcbor_map_start_encode(zse, counter); + } if (ok) { int rc = stat_mgmt_foreach_entry(zse, stat_name,