From 413f1be41e27f677afda4216b7db9c99ed295a12 Mon Sep 17 00:00:00 2001 From: Andrzej Puzdrowski Date: Fri, 9 Aug 2019 10:38:33 +0200 Subject: [PATCH] doc/reference/storage/settings: fix misleading examples Code snippets show misleading example of read_callback usage in h_set handlers. This patch aligns snippets to implemented behavior. fixes #15451 Signed-off-by: Andrzej Puzdrowski --- doc/reference/storage/settings/settings.rst | 31 +++++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/doc/reference/storage/settings/settings.rst b/doc/reference/storage/settings/settings.rst index b2752282489..fa9b2a308fe 100644 --- a/doc/reference/storage/settings/settings.rst +++ b/doc/reference/storage/settings/settings.rst @@ -130,7 +130,9 @@ export functionality, for example, writing to the shell console). .. code-block:: c - static int8 foo_val; + #define DEFAULT_FOO_VAL_VALUE 1 + + static int8 foo_val = DEFAULT_FOO_VAL_VALUE; struct settings_handler my_conf = { .name = "foo", @@ -141,9 +143,25 @@ export functionality, for example, writing to the shell console). static int foo_settings_set(int argc, char **argv, settings_read_cb read_cb, void *cb_arg) { + int rc; + if (argc == 1) { if (!strcmp(argv[0], "bar")) { - return read_cb(cb_arg, &foo_val, sizeof(foo_val)); + rc = read_cb(cb_arg, &foo_val, sizeof(foo_val)); + if (rc >= 0) { + /* key-value pair was properly read. + * rc contains value length. + * key-value is deleted if length equals 0. + * Let's return success. + */ + if (rc == 0) { + /* set the default value as its key is deleted */ + foo_val = DEFAULT_FOO_VAL_VALUE; + } + return 0; + } + /* read-out error */ + return rc; } } @@ -181,9 +199,16 @@ up from where it was before restart. static int foo_settings_set(int argc, char **argv, settings_read_cb read_cb, void *cb_arg) { + int rc; + if (argc == 1) { if (!strcmp(argv[0], "bar")) { - return read_cb(cb_arg, &foo_val, sizeof(foo_val)); + rc = read_cb(cb_arg, &foo_val, sizeof(foo_val)); + if (rc >= 0) { + return 0; + } + + return rc; } }