mgmt: mcumgr: Make returning rc responses when status is OK legacy

This change changes the previous mcumgr behaviour of return result
codes when the status is 0 (OK) to being legacy behaviour, instead
it will skip the rc field for these responses. If there is only an
rc field with status 0 to return, then mcumgr will now instead just
return an empty map.

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
This commit is contained in:
Jamie McCrae 2022-10-20 14:27:17 +01:00 committed by Carles Cufí
commit b4c2b57f1b
4 changed files with 82 additions and 59 deletions

View file

@ -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"

View file

@ -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) {

View file

@ -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;
}

View file

@ -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,