You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/10/01 18:17:34 UTC

[GitHub] mkiiskila closed pull request #1398: sys/config; add conf_get_stored_value(), which fetches the value of an item from persisted storage.

mkiiskila closed pull request #1398: sys/config; add conf_get_stored_value(), which fetches the value of an item from persisted storage.
URL: https://github.com/apache/mynewt-core/pull/1398
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/sys/config/include/config/config.h b/sys/config/include/config/config.h
index fc324c5ed3..124737ce37 100644
--- a/sys/config/include/config/config.h
+++ b/sys/config/include/config/config.h
@@ -259,6 +259,21 @@ int conf_set_value(char *name, char *val_str);
  */
 char *conf_get_value(char *name, char *buf, int buf_len);
 
+/**
+ * Get stored value of configuration item identified by @p name.
+ * This traverses the configuration area(s), and copies the value
+ * of the latest value.
+ *
+ * Value is copied to @p buf, the maximum number of bytes it will copy is
+ * limited by @p buf_len.
+ *
+ * @param name Name/key of the configuration item.
+ * @param val_str Value of the configuration item.
+ *
+ * @return 0 on success, non-zero on failure.
+ */
+int conf_get_stored_value(char *name, char *buf, int buf_len);
+
 /**
  * Call commit for all configuration handler. This should apply all
  * configuration which has been set, but not applied yet.
diff --git a/sys/config/src/config_store.c b/sys/config/src/config_store.c
index cbb882557d..64a39b76bf 100644
--- a/sys/config/src/config_store.c
+++ b/sys/config/src/config_store.c
@@ -31,6 +31,12 @@ struct conf_dup_check_arg {
     int is_dup;
 };
 
+struct conf_get_val_arg {
+    const char *name;
+    char val[CONF_MAX_VAL_LEN + 1];
+    int seen;
+};
+
 struct conf_store_head conf_load_srcs;
 struct conf_store *conf_save_dst;
 static bool conf_loading;
@@ -105,6 +111,54 @@ conf_set_from_storage(void)
     return conf_loading;
 }
 
+static void
+conf_get_value_cb(char *name, char *val, void *cb_arg)
+{
+    struct conf_get_val_arg *cgva = (struct conf_get_val_arg *)cb_arg;
+
+    if (strcmp(name, cgva->name)) {
+        return;
+    }
+    cgva->seen = 1;
+    if (!val) {
+        cgva->val[0] = '\0';
+    } else {
+        strncpy(cgva->val, val, sizeof(cgva->val) - 1);
+    }
+}
+
+int
+conf_get_stored_value(char *name, char *buf, int buf_len)
+{
+    struct conf_store *cs;
+    struct conf_get_val_arg cgva;
+    int val_len;
+
+    cgva.name = name;
+    cgva.val[0] = '\0';
+    cgva.val[sizeof(cgva.val) - 1] = '\0';
+    cgva.seen = 0;
+
+    /*
+     * for every config store
+     */
+    conf_lock();
+    SLIST_FOREACH(cs, &conf_load_srcs, cs_next) {
+        cs->cs_itf->csi_load(cs, conf_get_value_cb, &cgva);
+    }
+    conf_unlock();
+
+    if (!cgva.seen) {
+        return OS_ENOENT;
+    }
+    val_len = strlen(cgva.val);
+    if (buf_len < val_len) {
+        return OS_EINVAL;
+    }
+    strcpy(buf, cgva.val);
+    return 0;
+}
+
 static void
 conf_dup_check_cb(char *name, char *val, void *cb_arg)
 {
diff --git a/sys/config/test-fcb/src/conf_test_fcb.c b/sys/config/test-fcb/src/conf_test_fcb.c
index 1b8e33ec6b..df06472c4f 100644
--- a/sys/config/test-fcb/src/conf_test_fcb.c
+++ b/sys/config/test-fcb/src/conf_test_fcb.c
@@ -333,6 +333,7 @@ TEST_CASE_DECL(config_test_save_3_fcb)
 TEST_CASE_DECL(config_test_compress_reset)
 TEST_CASE_DECL(config_test_save_one_fcb)
 TEST_CASE_DECL(config_test_custom_compress)
+TEST_CASE_DECL(config_test_get_stored_fcb)
 
 TEST_SUITE(config_test_all)
 {
@@ -365,6 +366,7 @@ TEST_SUITE(config_test_all)
     config_test_custom_compress();
 
     config_test_save_one_fcb();
+    config_test_get_stored_fcb();
 }
 
 #if MYNEWT_VAL(SELFTEST)
diff --git a/sys/config/test-fcb/src/testcases/config_test_get_stored.c b/sys/config/test-fcb/src/testcases/config_test_get_stored.c
new file mode 100644
index 0000000000..f94b7dd4bc
--- /dev/null
+++ b/sys/config/test-fcb/src/testcases/config_test_get_stored.c
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include "conf_test_fcb.h"
+
+TEST_CASE(config_test_get_stored_fcb)
+{
+    int rc;
+    struct conf_fcb cf;
+    char stored_val[32];
+
+    config_wipe_srcs();
+    config_wipe_fcb(fcb_areas, sizeof(fcb_areas) / sizeof(fcb_areas[0]));
+
+    cf.cf_fcb.f_magic = MYNEWT_VAL(CONFIG_FCB_MAGIC);
+    cf.cf_fcb.f_sectors = fcb_areas;
+    cf.cf_fcb.f_sector_cnt = sizeof(fcb_areas) / sizeof(fcb_areas[0]);
+
+    rc = conf_fcb_src(&cf);
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_fcb_dst(&cf);
+    TEST_ASSERT(rc == 0);
+
+    test_export_block = 0;
+    val8 = 33;
+    rc = conf_save();
+    TEST_ASSERT(rc == 0);
+
+    /*
+     * Nonexistent key
+     */
+    rc = conf_get_stored_value("random/name", stored_val, sizeof(stored_val));
+    TEST_ASSERT(rc == OS_ENOENT);
+
+    rc = conf_get_stored_value("myfoo/mybar", stored_val, sizeof(stored_val));
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(atoi(stored_val) == 33);
+
+    rc = conf_save_one("myfoo/mybar", "42");
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_get_stored_value("myfoo/mybar", stored_val, sizeof(stored_val));
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(atoi(stored_val) == 42);
+
+    val8 = 31;
+    rc = conf_save();
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_get_stored_value("myfoo/mybar", stored_val, sizeof(stored_val));
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(atoi(stored_val) == 31);
+
+    /*
+     * Too small of a buffer
+     */
+    rc = conf_get_stored_value("myfoo/mybar", stored_val, 1);
+    TEST_ASSERT(rc == OS_EINVAL);
+
+    test_export_block = 1;
+}
diff --git a/sys/config/test-nffs/src/conf_test_nffs.c b/sys/config/test-nffs/src/conf_test_nffs.c
index 9f7e80cf5b..8bb1bbac17 100644
--- a/sys/config/test-nffs/src/conf_test_nffs.c
+++ b/sys/config/test-nffs/src/conf_test_nffs.c
@@ -339,6 +339,7 @@ TEST_CASE_DECL(config_test_small_file)
 TEST_CASE_DECL(config_test_multiple_in_file)
 TEST_CASE_DECL(config_test_save_in_file)
 TEST_CASE_DECL(config_test_save_one_file)
+TEST_CASE_DECL(config_test_get_stored_file)
 
 TEST_SUITE(config_test_all)
 {
@@ -365,6 +366,7 @@ TEST_SUITE(config_test_all)
     config_test_save_in_file();
 
     config_test_save_one_file();
+    config_test_get_stored_file();
 }
 
 #if MYNEWT_VAL(SELFTEST)
diff --git a/sys/config/test-nffs/src/testcases/config_test_get_stored.c b/sys/config/test-nffs/src/testcases/config_test_get_stored.c
new file mode 100644
index 0000000000..c9c368aa85
--- /dev/null
+++ b/sys/config/test-nffs/src/testcases/config_test_get_stored.c
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include "conf_test_nffs.h"
+
+TEST_CASE(config_test_get_stored_file)
+{
+    int rc;
+    struct conf_file cf;
+    char stored_val[32];
+
+    config_wipe_srcs();
+    rc = fs_mkdir("/config");
+    TEST_ASSERT(rc == 0 || rc == FS_EEXIST);
+
+    cf.cf_name = "/config/blah";
+    rc = conf_file_src(&cf);
+    TEST_ASSERT(rc == 0);
+    rc = conf_file_dst(&cf);
+    TEST_ASSERT(rc == 0);
+
+    test_export_block = 0;
+    val8 = 33;
+    rc = conf_save();
+    TEST_ASSERT(rc == 0);
+
+    /*
+     * Nonexistent key
+     */
+    rc = conf_get_stored_value("random/name", stored_val, sizeof(stored_val));
+    TEST_ASSERT(rc == OS_ENOENT);
+
+    rc = conf_get_stored_value("myfoo/mybar", stored_val, sizeof(stored_val));
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(atoi(stored_val) == 33);
+
+    rc = conf_save_one("myfoo/mybar", "42");
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_get_stored_value("myfoo/mybar", stored_val, sizeof(stored_val));
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(atoi(stored_val) == 42);
+
+    val8 = 31;
+    rc = conf_save();
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_get_stored_value("myfoo/mybar", stored_val, sizeof(stored_val));
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(atoi(stored_val) == 31);
+
+    /*
+     * Too small of a buffer
+     */
+    rc = conf_get_stored_value("myfoo/mybar", stored_val, 1);
+    TEST_ASSERT(rc == OS_EINVAL);
+
+    test_export_block = 1;
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services