mynewt-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject [2/2] incubator-mynewt-core git commit: Start adding storage options for persisting config variables. Start by allowing 'load' from files.
Date Wed, 23 Mar 2016 17:26:54 GMT
Start adding storage options for persisting config variables.
Start by allowing 'load' from files.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/b799a81e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/b799a81e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/b799a81e

Branch: refs/heads/develop
Commit: b799a81ec840b4ba4f69b9f13a3a21a40aedc67d
Parents: 7e4d1a5
Author: Marko Kiiskila <marko@runtime.io>
Authored: Wed Mar 23 10:25:57 2016 -0700
Committer: Marko Kiiskila <marko@runtime.io>
Committed: Wed Mar 23 10:25:57 2016 -0700

----------------------------------------------------------------------
 apps/test/src/test.c                    |   2 +-
 sys/config/include/config/config.h      |  28 +++-
 sys/config/include/config/config_fcb.h  |  36 ++++++
 sys/config/include/config/config_file.h |  36 ++++++
 sys/config/include/config/config_test.h |  24 ----
 sys/config/pkg.yml                      |  11 ++
 sys/config/src/config.c                 |  15 +--
 sys/config/src/config_fcb.c             |  38 ++++++
 sys/config/src/config_file.c            | 186 +++++++++++++++++++++++++++
 sys/config/src/config_nmgr.c            |  28 +---
 sys/config/src/config_parse_line.c      |  54 ++++++++
 sys/config/src/config_priv.h            |   6 +-
 sys/config/src/config_store.c           |  70 ++++++++++
 sys/config/src/test/conf_test.c         | 124 +++++++++++++++---
 sys/config/src/test/conf_test.h         |  25 ++++
 sys/config/src/test/conf_test_suite.c   |  39 ++++++
 sys/config/src/test/config_test.h       |  24 ++++
 17 files changed, 665 insertions(+), 81 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b799a81e/apps/test/src/test.c
----------------------------------------------------------------------
diff --git a/apps/test/src/test.c b/apps/test/src/test.c
index 57c6418..bde2fef 100644
--- a/apps/test/src/test.c
+++ b/apps/test/src/test.c
@@ -23,7 +23,7 @@
 #include "bootutil/bootutil_test.h"
 #include "testutil/testutil.h"
 #include "mbedtls/mbedtls_test.h"
-#include "config/config_test.h"
+#include "config/../../src/test/config_test.h"
 
 extern int util_test_all(void);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b799a81e/sys/config/include/config/config.h
----------------------------------------------------------------------
diff --git a/sys/config/include/config/config.h b/sys/config/include/config/config.h
index 73b7c74..463d6b5 100644
--- a/sys/config/include/config/config.h
+++ b/sys/config/include/config/config.h
@@ -6,7 +6,7 @@
  * 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,
@@ -16,13 +16,15 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#ifndef __UTIL_CONFIG_H_
-#define __UTIL_CONFIG_H_
+#ifndef __SYS_CONFIG_H_
+#define __SYS_CONFIG_H_
 
 #include <os/queue.h>
 #include <stdint.h>
 
 #define CONF_MAX_DIR_DEPTH	8	/* max depth of config tree */
+#define CONF_MAX_NAME_LEN	(8 * CONF_MAX_DIR_DEPTH)
+#define CONF_MAX_VAL_LEN	256
 #define CONF_NAME_SEPARATOR	"/"
 
 #define CONF_NMGR_OP		0
@@ -63,4 +65,22 @@ char *conf_str_from_value(enum conf_type type, void *vp, char *buf,
 #define CONF_VALUE_SET(str, type, val)                                  \
     conf_value_from_str((str), (type), &(val), sizeof(val))
 
-#endif /* __UTIL_CONFIG_H_ */
+/*
+ * Persisting config.
+ */
+typedef void (*load_cb)(char *name, char *val, void *cb_arg);
+
+struct conf_store;
+struct conf_store_itf {
+    int (*csi_load)(struct conf_store *ci, load_cb cb, void *cb_arg);
+    int (*csi_save)(struct conf_store *ci, char *name, char *value);
+};
+
+struct conf_store {
+    SLIST_ENTRY(conf_store) cs_next;
+    const struct conf_store_itf *cs_itf;
+};
+
+void conf_src_register(struct conf_store *cs);
+
+#endif /* __SYS_CONFIG_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b799a81e/sys/config/include/config/config_fcb.h
----------------------------------------------------------------------
diff --git a/sys/config/include/config/config_fcb.h b/sys/config/include/config/config_fcb.h
new file mode 100644
index 0000000..55c9ef9
--- /dev/null
+++ b/sys/config/include/config/config_fcb.h
@@ -0,0 +1,36 @@
+/**
+ * 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.
+ */
+#ifndef __SYS_CONFIG_FCB_H_
+#define __SYS_CONFIG_FCB_H_
+
+#ifdef FCB_PRESENT
+
+#include <fcb/fcb.h>
+#include "config/config.h"
+
+struct conf_file {
+    struct cf_storage cf_itf;
+    struct fcb cf_fcb;
+};
+
+int conf_fcb_register(struct conf_fcb *fcb);
+
+#endif
+
+#endif /* __SYS_CONFIG_FCB_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b799a81e/sys/config/include/config/config_file.h
----------------------------------------------------------------------
diff --git a/sys/config/include/config/config_file.h b/sys/config/include/config/config_file.h
new file mode 100644
index 0000000..13fe769
--- /dev/null
+++ b/sys/config/include/config/config_file.h
@@ -0,0 +1,36 @@
+/**
+ * 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.
+ */
+#ifndef __SYS_CONFIG_FILE_H_
+#define __SYS_CONFIG_FILE_H_
+
+#ifdef FS_PRESENT
+
+#include <fs/fs.h>
+#include "config/config.h"
+
+struct conf_file {
+    struct conf_store cf_store;
+    const char *cf_name;
+};
+
+int conf_file_register(struct conf_file *);
+
+#endif
+
+#endif /* __SYS_CONFIG_FILE_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b799a81e/sys/config/include/config/config_test.h
----------------------------------------------------------------------
diff --git a/sys/config/include/config/config_test.h b/sys/config/include/config/config_test.h
deleted file mode 100644
index da531af..0000000
--- a/sys/config/include/config/config_test.h
+++ /dev/null
@@ -1,24 +0,0 @@
-/**
- * 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.
- */
-#ifndef _CONFIG_TEST_H_
-#define _CONFIG_TEST_H_
-
-int config_test_all();
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b799a81e/sys/config/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/config/pkg.yml b/sys/config/pkg.yml
index b0d7a84..6c03dd6 100644
--- a/sys/config/pkg.yml
+++ b/sys/config/pkg.yml
@@ -34,3 +34,14 @@ pkg.cflags.SHELL: -DSHELL_PRESENT
 pkg.deps.NEWTMGR:
     - libs/newtmgr
 pkg.cflags.NEWTMGR: -DNEWTMGR_PRESENT
+pkg.deps.FCB:
+    - sys/fcb
+pkg.cflags.FCB: -DFCB_PRESENT
+pkg.deps.FS:
+    - fs/fs
+    - libs/json
+pkg.cflags.FS: -DFS_PRESENT
+
+pkg.deps.TEST:
+    - fs/nffs
+    - sys/fcb

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b799a81e/sys/config/src/config.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config.c b/sys/config/src/config.c
index 5586896..39ef389 100644
--- a/sys/config/src/config.c
+++ b/sys/config/src/config.c
@@ -6,7 +6,7 @@
  * 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,
@@ -49,18 +49,6 @@ conf_register(struct conf_handler *handler)
     return 0;
 }
 
-int
-conf_load(void)
-{
-    /*
-     * for every config source
-     *    load config
-     *    apply config
-     *    commit all
-     */
-    return 0;
-}
-
 /*
  * Find conf_handler based on name.
  */
@@ -247,3 +235,4 @@ conf_commit(char *name)
         return 0;
     }
 }
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b799a81e/sys/config/src/config_fcb.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_fcb.c b/sys/config/src/config_fcb.c
new file mode 100644
index 0000000..3ef72a0
--- /dev/null
+++ b/sys/config/src/config_fcb.c
@@ -0,0 +1,38 @@
+/**
+ * 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.
+ */
+
+#ifdef FCB_PRESENT
+
+#include "config/config.h"
+#include "config/config_fcb"
+#include "config_priv.h"
+
+int
+conf_fcb_register(struct conf_fcb *cf)
+{
+    int rc;
+
+    rc = fcb_init(&cf->cf_fcb);
+    if (rc) {
+        return -1;
+    }
+    return conf_src_register(&cf->cf_itf);
+}
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b799a81e/sys/config/src/config_file.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_file.c b/sys/config/src/config_file.c
new file mode 100644
index 0000000..96c2632
--- /dev/null
+++ b/sys/config/src/config_file.c
@@ -0,0 +1,186 @@
+/**
+ * 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.
+ */
+
+#ifdef FS_PRESENT
+
+#include <string.h>
+
+#include <fs/fs.h>
+#include <json/json.h>
+
+#include "config/config.h"
+#include "config/config_file.h"
+#include "config_priv.h"
+
+static int conf_file_load(struct conf_store *, load_cb cb, void *cb_arg);
+static int conf_file_save(struct conf_store *, char *name, char *value);
+
+static struct conf_store_itf conf_file_itf = {
+    .csi_load = conf_file_load,
+    .csi_save = conf_file_save
+};
+
+struct conf_file_jbuf {
+    struct json_buffer jb;
+    int off;
+    int len;
+    char *buf;
+};
+
+/*
+ * Register a file to be a source of configuration.
+ */
+int
+conf_file_register(struct conf_file *cf)
+{
+    if (!cf->cf_name) {
+        return -1;
+    }
+    cf->cf_store.cs_itf = &conf_file_itf;
+    conf_src_register(&cf->cf_store);
+    return 0;
+}
+
+int
+conf_getnext_line(struct fs_file *file, char *buf, int blen, uint32_t *loc)
+{
+    int rc;
+    char *end;
+    uint32_t len;
+
+    rc = fs_seek(file, *loc);
+    if (rc < 0) {
+        *loc = 0;
+        return -1;
+    }
+    rc = fs_read(file, blen, buf, &len);
+    if (rc < 0 || len == 0) {
+        *loc = 0;
+        return -1;
+    }
+    if (len == blen) {
+        rc--;
+    }
+    buf[len] = '\0';
+
+    end = strchr(buf, '}');
+    if (end) {
+        *(end + 1) = '\0';
+        blen = end + 1 - buf;
+        *loc += blen;
+        return blen;
+    } else {
+        *loc += blen;
+        return -1;
+    }
+}
+
+static char
+conf_file_jb_read_next(struct json_buffer *jb)
+{
+    struct conf_file_jbuf *cfj = (struct conf_file_jbuf *)jb;
+
+    if (cfj->off >= cfj->len) {
+        return '\0';
+    }
+    return cfj->buf[cfj->off++];
+}
+
+static char
+conf_file_jb_read_prev(struct json_buffer *jb)
+{
+    struct conf_file_jbuf *cfj = (struct conf_file_jbuf *)jb;
+
+    if (cfj->off == 0) {
+        return '\0';
+    }
+    return cfj->buf[--cfj->off];
+}
+
+static int
+conf_file_jb_readn(struct json_buffer *jb, char *buf, int size)
+{
+    struct conf_file_jbuf *cfj = (struct conf_file_jbuf *)jb;
+    int len;
+
+    len = cfj->len - cfj->off;
+    len = size > len ? len : size;
+
+    memcpy(buf, &cfj->buf[cfj->off], len);
+    return len;
+}
+
+/*
+ * Called to load configuration items. cb must be called for every configuration
+ * item found.
+ */
+static int
+conf_file_load(struct conf_store *cs, load_cb cb, void *cb_arg)
+{
+    struct conf_file *cf = (struct conf_file *)cs;
+    struct fs_file *file;
+    uint32_t loc;
+    struct conf_file_jbuf cfj;
+    char tmpbuf[CONF_MAX_NAME_LEN + CONF_MAX_VAL_LEN + 32];
+    char name_str[CONF_MAX_NAME_LEN];
+    char val_str[CONF_MAX_VAL_LEN];
+    int rc;
+
+    rc = fs_open(cf->cf_name, FS_ACCESS_READ, &file);
+    if (rc != FS_EOK) {
+        return -1;
+    }
+
+    loc = 0;
+    cfj.jb.jb_read_next = conf_file_jb_read_next;
+    cfj.jb.jb_read_prev = conf_file_jb_read_prev;
+    cfj.jb.jb_readn = conf_file_jb_readn;
+    cfj.buf = tmpbuf;
+    while (1) {
+        rc = conf_getnext_line(file, tmpbuf, sizeof(tmpbuf), &loc);
+        if (loc == 0) {
+            break;
+        }
+        if (rc < 0) {
+            continue;
+        }
+        cfj.off = 0;
+        cfj.len = rc;
+        rc = conf_parse_line(&cfj.jb, name_str, sizeof(name_str), val_str,
+          sizeof(val_str));
+        if (rc != 0) {
+            continue;
+        }
+        cb(name_str, val_str, cb_arg);
+    }
+    fs_close(file);
+    return rc;
+}
+
+/*
+ * Called to save a configuration item. Needs to override previous value for
+ * variable identified by 'name'.
+ */
+static int
+conf_file_save(struct conf_store *cs, char *name, char *value)
+{
+    return -1;
+}
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b799a81e/sys/config/src/config_nmgr.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_nmgr.c b/sys/config/src/config_nmgr.c
index 7a2cf27..ec8b201 100644
--- a/sys/config/src/config_nmgr.c
+++ b/sys/config/src/config_nmgr.c
@@ -6,7 +6,7 @@
  * 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,
@@ -43,7 +43,7 @@ static struct nmgr_group conf_nmgr_group = {
 static int
 conf_nmgr_read(struct nmgr_jbuf *njb)
 {
-    char tmp_str[max(CONF_MAX_DIR_DEPTH * 8, 16)];
+    char tmp_str[CONF_MAX_NAME_LEN];
     char *val_str;
     const struct json_attr_t attr[2] = {
         [0] = {
@@ -80,28 +80,12 @@ conf_nmgr_read(struct nmgr_jbuf *njb)
 static int
 conf_nmgr_write(struct nmgr_jbuf *njb)
 {
-    char name_str[CONF_MAX_DIR_DEPTH * 8];
-    char val_str[256];
-    struct json_attr_t attr[3] = {
-        [0] = {
-            .attribute = "name",
-            .type = t_string,
-            .addr.string = name_str,
-            .len = sizeof(name_str)
-        },
-        [1] = {
-            .attribute = "val",
-            .type = t_string,
-            .addr.string = val_str,
-            .len = sizeof(val_str)
-        },
-        [2] = {
-            .attribute = NULL
-        }
-    };
     int rc;
+    char name_str[CONF_MAX_NAME_LEN];
+    char val_str[CONF_MAX_VAL_LEN];
 
-    rc = json_read_object(&njb->njb_buf, attr);
+    rc = conf_parse_line(&njb->njb_buf, name_str, sizeof(name_str), val_str,
+      sizeof(val_str));
     if (rc) {
         return OS_EINVAL;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b799a81e/sys/config/src/config_parse_line.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_parse_line.c b/sys/config/src/config_parse_line.c
new file mode 100644
index 0000000..bce6ab0
--- /dev/null
+++ b/sys/config/src/config_parse_line.c
@@ -0,0 +1,54 @@
+/**
+ * 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 "config/config.h"
+#include "config_priv.h"
+
+#include <json/json.h>
+
+int
+conf_parse_line(struct json_buffer *jb, char *name, int nlen, char *value,
+  int vlen)
+{
+    const struct json_attr_t val_attr[3] = {
+        [0] = {
+            .attribute = "name",
+            .type = t_string,
+            .addr.string = name,
+            .len = nlen
+        },
+        [1] = {
+            .attribute = "val",
+            .type = t_string,
+            .addr.string = value,
+            .len = vlen
+        },
+        [2] = {
+            .attribute = NULL
+        }
+    };
+    int rc;
+
+    rc = json_read_object(jb, val_attr);
+    if (rc) {
+        return rc;
+    }
+    return 0;
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b799a81e/sys/config/src/config_priv.h
----------------------------------------------------------------------
diff --git a/sys/config/src/config_priv.h b/sys/config/src/config_priv.h
index 2ce1dae..09618e9 100644
--- a/sys/config/src/config_priv.h
+++ b/sys/config/src/config_priv.h
@@ -6,7 +6,7 @@
  * 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,
@@ -23,4 +23,8 @@
 int conf_cli_register(void);
 int conf_nmgr_register(void);
 
+struct json_buffer;
+int conf_parse_line(struct json_buffer *jb, char *name, int nlen, char *value,
+  int vlen);
+
 #endif /* __CONFIG_PRIV_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b799a81e/sys/config/src/config_store.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_store.c b/sys/config/src/config_store.c
new file mode 100644
index 0000000..a31196c
--- /dev/null
+++ b/sys/config/src/config_store.c
@@ -0,0 +1,70 @@
+/**
+ * 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 <string.h>
+#include <stdio.h>
+
+#include <os/os.h>
+
+#include "config/config.h"
+#include "config_priv.h"
+
+SLIST_HEAD(, conf_store) conf_load_srcs =
+    SLIST_HEAD_INITIALIZER(&conf_load_srcs);
+
+void
+conf_src_register(struct conf_store *cs)
+{
+    struct conf_store *prev, *cur;
+
+    prev = NULL;
+    SLIST_FOREACH(cur, &conf_load_srcs, cs_next) {
+        prev = cur;
+    }
+    if (!prev) {
+        SLIST_INSERT_HEAD(&conf_load_srcs, cs, cs_next);
+    } else {
+        SLIST_INSERT_AFTER(prev, cs, cs_next);
+    }
+}
+
+static void
+conf_load_cb(char *name, char *val, void *cb_arg)
+{
+    conf_set_value(name, val);
+}
+
+int
+conf_load(void)
+{
+    struct conf_store *cs;
+
+    /*
+     * for every config store
+     *    load config
+     *    apply config
+     *    commit all
+     */
+
+    SLIST_FOREACH(cs, &conf_load_srcs, cs_next) {
+        cs->cs_itf->csi_load(cs, conf_load_cb, NULL);
+    }
+    return 0;
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b799a81e/sys/config/src/test/conf_test.c
----------------------------------------------------------------------
diff --git a/sys/config/src/test/conf_test.c b/sys/config/src/test/conf_test.c
index 427cac0..9a8ebb7 100644
--- a/sys/config/src/test/conf_test.c
+++ b/sys/config/src/test/conf_test.c
@@ -6,7 +6,7 @@
  * 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,
@@ -21,7 +21,12 @@
 
 #include <os/os.h>
 #include <testutil/testutil.h>
-#include <config/config.h>
+#include <nffs/nffs.h>
+#include <fs/fs.h>
+#include <fs/fsutil.h>
+#include "config/config.h"
+#include "config/config_file.h"
+#include "test/config_test.h"
 
 static uint8_t val8;
 
@@ -29,6 +34,8 @@ static int test_get_called;
 static int test_set_called;
 static int test_commit_called;
 
+extern SLIST_HEAD(, conf_store) conf_load_srcs; /* config_store.c */
+
 static char *
 ctest_handle_get(int argc, char **argv, char *val, int val_len_max)
 {
@@ -178,6 +185,102 @@ TEST_CASE(config_test_commit)
     ctest_clear_call_state();
 }
 
+static const struct nffs_area_desc config_nffs[] = {
+    { 0x00000000, 16 * 1024 },
+    { 0x00004000, 16 * 1024 },
+    { 0x00008000, 16 * 1024 },
+    { 0x0000c000, 16 * 1024 },
+};
+
+static void config_setup_nffs(void)
+{
+    int rc;
+
+    rc = nffs_init();
+    TEST_ASSERT(rc == 0);
+    rc = nffs_format(config_nffs);
+    TEST_ASSERT(rc == 0);
+}
+
+static void config_wipe_srcs(void)
+{
+    SLIST_INIT(&conf_load_srcs);
+}
+
+TEST_CASE(config_test_empty_file)
+{
+    int rc;
+    struct conf_file cf_mfg;
+    struct conf_file cf_running;
+    const char cf_mfg_test[] = "";
+    const char cf_running_test[] = "\n\n";
+
+    config_wipe_srcs();
+
+    cf_mfg.cf_name = "/config/mfg";
+    cf_running.cf_name = "/config/running";
+
+    rc = conf_file_register(&cf_mfg);
+    TEST_ASSERT(rc == 0);
+    rc = conf_file_register(&cf_running);
+
+    /*
+     * No files
+     */
+    conf_load();
+
+    rc = fs_mkdir("/config");
+    TEST_ASSERT(rc == 0);
+
+    rc = fsutil_write_file("/config/mfg", cf_mfg_test, sizeof(cf_mfg_test));
+    TEST_ASSERT(rc == 0);
+
+    rc = fsutil_write_file("/config/running", cf_running_test,
+      sizeof(cf_running_test));
+    TEST_ASSERT(rc == 0);
+
+    conf_load();
+    config_wipe_srcs();
+    ctest_clear_call_state();
+}
+
+TEST_CASE(config_test_small_file)
+{
+    int rc;
+    struct conf_file cf_mfg;
+    struct conf_file cf_running;
+    const char cf_mfg_test[] = "{\"name\":\"myfoo/mybar\",\"val\":\"1\"}";
+    const char cf_running_test[] = "{\"name\":\"myfoo/mybar\",\"val\":\"8\"}";
+
+    config_wipe_srcs();
+
+    cf_mfg.cf_name = "/config/mfg";
+    cf_running.cf_name = "/config/running";
+
+    rc = conf_file_register(&cf_mfg);
+    TEST_ASSERT(rc == 0);
+    rc = conf_file_register(&cf_running);
+
+    rc = fsutil_write_file("/config/mfg", cf_mfg_test, sizeof(cf_mfg_test));
+    TEST_ASSERT(rc == 0);
+
+    conf_load();
+    TEST_ASSERT(test_set_called);
+    TEST_ASSERT(val8 == 1);
+
+    ctest_clear_call_state();
+
+    rc = fsutil_write_file("/config/running", cf_running_test,
+      sizeof(cf_running_test));
+    TEST_ASSERT(rc == 0);
+
+    conf_load();
+    TEST_ASSERT(test_set_called);
+    TEST_ASSERT(val8 == 8);
+
+    ctest_clear_call_state();
+}
+
 TEST_SUITE(config_test_all)
 {
     config_empty_lookups();
@@ -185,20 +288,9 @@ TEST_SUITE(config_test_all)
     config_test_getset_unknown();
     config_test_getset_int();
     config_test_commit();
-}
-
-#ifdef MYNEWT_SELFTEST
-
-int
-main(int argc, char **argv)
-{
-    tu_config.tc_print_results = 1;
-    tu_init();
-
-    conf_init();
-    config_test_all();
 
-    return tu_any_failed;
+    config_setup_nffs();
+    config_test_empty_file();
+    config_test_small_file();
 }
 
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b799a81e/sys/config/src/test/conf_test.h
----------------------------------------------------------------------
diff --git a/sys/config/src/test/conf_test.h b/sys/config/src/test/conf_test.h
new file mode 100644
index 0000000..b50ff2f
--- /dev/null
+++ b/sys/config/src/test/conf_test.h
@@ -0,0 +1,25 @@
+/**
+ * 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.
+ */
+
+#ifndef _CONF_TEST_H_
+#define _CONF_TEST_H_
+
+void config_test_all(void);
+
+#endif /* _CONF_TEST_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b799a81e/sys/config/src/test/conf_test_suite.c
----------------------------------------------------------------------
diff --git a/sys/config/src/test/conf_test_suite.c b/sys/config/src/test/conf_test_suite.c
new file mode 100644
index 0000000..ef059cb
--- /dev/null
+++ b/sys/config/src/test/conf_test_suite.c
@@ -0,0 +1,39 @@
+/**
+ * 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 <os/os.h>
+#include <testutil/testutil.h>
+#include "config/config.h"
+#include "test/config_test.h"
+
+#ifdef MYNEWT_SELFTEST
+
+int
+main(int argc, char **argv)
+{
+    tu_config.tc_print_results = 1;
+    tu_init();
+
+    conf_init();
+    config_test_all();
+
+    return tu_any_failed;
+}
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b799a81e/sys/config/src/test/config_test.h
----------------------------------------------------------------------
diff --git a/sys/config/src/test/config_test.h b/sys/config/src/test/config_test.h
new file mode 100644
index 0000000..da531af
--- /dev/null
+++ b/sys/config/src/test/config_test.h
@@ -0,0 +1,24 @@
+/**
+ * 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.
+ */
+#ifndef _CONFIG_TEST_H_
+#define _CONFIG_TEST_H_
+
+int config_test_all();
+
+#endif


Mime
View raw message