Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

settings: shell: improve reading and writing string values #85786

Merged

Conversation

Damian-Nordic
Copy link
Collaborator

Make reading and writing string values more flexible:

  1. Eliminate the intermediate buffer when saving a string setting. This needlessly limited the maximum string length that could be saved using the shell command.
  2. Do not add nor assume that a string saved in the settings includes the null-terminator. The settings subsystem uses metadata for encoding the value length, so there it is redundant to also store the null-terminator in flash.

By the way, also make sure the command handlers only return -EINVAL and -ENOEXEC error codes as documented in the handler type description.

Make reading and writing string values more flexible:
1. Eliminate the intermediate buffer when saving a string
   setting. This needlessly limited the maximum string
   length that could be saved using the shell command.
2. Do not add nor assume that a string saved in the settings
   includes the null-terminator. The settings subsystem uses
   metadata for encoding the value length, so there it is
   redundant to also store the null-terminator in flash.

By the way, also make sure the command handlers only return
-EINVAL and -ENOEXEC error codes as documented in the
handler type description.

Signed-off-by: Damian Krolik <damian.krolik@nordicsemi.no>
@zephyrbot zephyrbot added the area: Settings Settings subsystem label Feb 14, 2025
@Damian-Nordic
Copy link
Collaborator Author

@danielstuart14 Let me know if you're OK with changing the behavior that you added. If not, I can keep adding the null-terminator in the write command :).

@danielstuart14
Copy link
Contributor

@danielstuart14 Let me know if you're OK with changing the behavior that you added. If not, I can keep adding the null-terminator in the write command :).

LGTM!

@@ -138,7 +143,7 @@ static int cmd_read(const struct shell *shell_ptr, size_t argc, char *argv[])
err = settings_parse_type(argv[1], &value_type);
if (err) {
shell_error(shell_ptr, "Invalid type: %s", argv[1]);
return err;
return -EINVAL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but settings_parse_type already returns -EINVAL, we should just propagate the error

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but this makes it a bit clearer that the handler does not return any other error code than the two allowed.

enum settings_value_types value_type = SETTINGS_VALUE_HEX;

if (argc > 3) {
err = settings_parse_type(argv[1], &value_type);
if (err) {
shell_error(shell_ptr, "Invalid type: %s", argv[1]);
return err;
return -EINVAL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

shell_error(shell_ptr, "Setting not found");
err = -ENOEXEC;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, also make sure the command handlers only return
-EINVAL and -ENOEXEC error codes as documented in the
handler type description.

Can you point me to this documentation ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
shell_print(params->shell_ptr, "%s", buffer);
shell_print(params->shell_ptr, "%.*s", (int)num_read_bytes, buffer);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the %.*s addition

@kartben kartben merged commit fa8eef0 into zephyrproject-rtos:main Mar 22, 2025
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Settings Settings subsystem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants