You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2016/04/07 21:01:13 UTC

[1/2] incubator-mynewt-core git commit: Add fcb_is_empty() to tell whether FCB has any data present or not.

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 1b91ac5e5 -> f2885ea6d


Add fcb_is_empty() to tell whether FCB has any data present or not.


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/e3aa39ce
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/e3aa39ce
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/e3aa39ce

Branch: refs/heads/develop
Commit: e3aa39ce345b0e0f4fa262e9aa3122f9bd695971
Parents: 1b91ac5
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Thu Apr 7 11:55:25 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Thu Apr 7 11:55:25 2016 -0700

----------------------------------------------------------------------
 sys/fcb/include/fcb/fcb.h | 5 +++++
 sys/fcb/src/fcb.c         | 7 +++++++
 2 files changed, 12 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e3aa39ce/sys/fcb/include/fcb/fcb.h
----------------------------------------------------------------------
diff --git a/sys/fcb/include/fcb/fcb.h b/sys/fcb/include/fcb/fcb.h
index 55332a9..4adae0e 100644
--- a/sys/fcb/include/fcb/fcb.h
+++ b/sys/fcb/include/fcb/fcb.h
@@ -107,4 +107,9 @@ int fcb_append_to_scratch(struct fcb *);
  */
 int fcb_free_sector_cnt(struct fcb *fcb);
 
+/*
+ * Whether FCB has any data.
+ */
+int fcb_is_empty(struct fcb *fcb);
+
 #endif /* __SYS_FLASHVAR_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e3aa39ce/sys/fcb/src/fcb.c
----------------------------------------------------------------------
diff --git a/sys/fcb/src/fcb.c b/sys/fcb/src/fcb.c
index 1c3ac3e..9c9eb44 100644
--- a/sys/fcb/src/fcb.c
+++ b/sys/fcb/src/fcb.c
@@ -105,6 +105,13 @@ fcb_free_sector_cnt(struct fcb *fcb)
     return i;
 }
 
+int
+fcb_is_empty(struct fcb *fcb)
+{
+    return (fcb->f_active.fe_area == fcb->f_oldest &&
+      fcb->f_active.fe_elem_off == sizeof(struct fcb_disk_area));
+}
+
 /*
  * Length of an element is encoded in 1 or 2 bytes.
  * 1 byte for lengths < 128 bytes, and 2 bytes for < 16384.


[2/2] incubator-mynewt-core git commit: Add FCB as an option to target logs to.

Posted by ma...@apache.org.
Add FCB as an option to target logs to.


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/f2885ea6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/f2885ea6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/f2885ea6

Branch: refs/heads/develop
Commit: f2885ea6dd1975f58df3bae05f790e3e86007095
Parents: e3aa39c
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Thu Apr 7 12:00:44 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Thu Apr 7 12:00:44 2016 -0700

----------------------------------------------------------------------
 sys/log/include/log/log.h   |   2 +
 sys/log/pkg.yml             |   6 ++
 sys/log/src/log_fcb.c       | 127 ++++++++++++++++++++++++++++++
 sys/log/src/test/log_test.c | 162 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 297 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f2885ea6/sys/log/include/log/log.h
----------------------------------------------------------------------
diff --git a/sys/log/include/log/log.h b/sys/log/include/log/log.h
index 68e7be2..df5ae90 100644
--- a/sys/log/include/log/log.h
+++ b/sys/log/include/log/log.h
@@ -141,6 +141,8 @@ int log_flush(struct log *log);
 /* Handler exports */
 int log_cbmem_handler_init(struct log_handler *, struct cbmem *);
 int log_console_handler_init(struct log_handler *);
+struct fcb;
+int log_fcb_handler_init(struct log_handler *handler, struct fcb *fcb);
 
 /* Private */
 #ifdef NEWTMGR_PRESENT

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f2885ea6/sys/log/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/log/pkg.yml b/sys/log/pkg.yml
index 1d40b43..2bf5036 100644
--- a/sys/log/pkg.yml
+++ b/sys/log/pkg.yml
@@ -32,7 +32,13 @@ pkg.deps.SHELL:
     - libs/shell
 pkg.deps.NEWTMGR:
     - libs/newtmgr
+pkg.deps.FCB:
+    - hw/hal
+    - sys/fcb
+pkg.deps.TEST:
+    - sys/fcb
 pkg.req_apis.SHELL:
     - console
 pkg.cflags.SHELL: -DSHELL_PRESENT
 pkg.cflags.NEWTMGR: -DNEWTMGR_PRESENT
+pkg.cflags.FCB: -DFCB_PRESENT

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f2885ea6/sys/log/src/log_fcb.c
----------------------------------------------------------------------
diff --git a/sys/log/src/log_fcb.c b/sys/log/src/log_fcb.c
new file mode 100644
index 0000000..f2316f2
--- /dev/null
+++ b/sys/log/src/log_fcb.c
@@ -0,0 +1,127 @@
+/**
+ * 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 <os/os.h>
+
+#include <hal/flash_map.h>
+#include <fcb/fcb.h>
+
+#include "log/log.h"
+
+static int
+log_fcb_append(struct log *log, void *buf, int len)
+{
+    struct fcb *fcb;
+    struct fcb_entry loc;
+    int rc;
+
+    fcb = (struct fcb *)log->l_log->log_arg;
+
+    while (1) {
+        rc = fcb_append(fcb, len, &loc);
+        if (rc == 0) {
+            break;
+        }
+        if (rc != FCB_ERR_NOSPACE) {
+            return rc;
+        }
+        rc = fcb_rotate(fcb);
+        if (rc) {
+            return rc;
+        }
+    }
+    rc = flash_area_write(loc.fe_area, loc.fe_data_off, buf, len);
+    if (rc) {
+        return rc;
+    }
+    fcb_append_finish(fcb, &loc);
+    return 0;
+}
+
+static int
+log_fcb_read(struct log *log, void *dptr, void *buf, uint16_t offset,
+  uint16_t len)
+{
+    struct fcb_entry *loc;
+    int rc;
+
+    loc = (struct fcb_entry *)dptr;
+
+    if (offset + len > loc->fe_data_len) {
+        len = loc->fe_data_len - offset;
+    }
+    rc = flash_area_read(loc->fe_area, loc->fe_data_off + offset, buf, len);
+    if (rc == 0) {
+        return len;
+    } else {
+        return 0;
+    }
+}
+
+static int
+log_fcb_walk(struct log *log, log_walk_func_t walk_func, void *arg)
+{
+    struct fcb *fcb;
+    struct fcb_entry loc;
+    int rc;
+
+    fcb = (struct fcb *)log->l_log->log_arg;
+
+    memset(&loc, 0, sizeof(loc));
+
+    while (fcb_getnext(fcb, &loc) == 0) {
+        rc = walk_func(log, arg, (void *) &loc, loc.fe_data_len);
+        if (rc == 1) {
+            break;
+        }
+    }
+    return 0;
+}
+
+static int
+log_fcb_flush(struct log *log)
+{
+    struct fcb *fcb;
+    int rc;
+
+    fcb = (struct fcb *)log->l_log->log_arg;
+    rc = 0;
+
+    while (!fcb_is_empty(fcb)) {
+        rc = fcb_rotate(fcb);
+        if (rc) {
+            break;
+        }
+    }
+    return rc;
+}
+
+int
+log_fcb_handler_init(struct log_handler *handler, struct fcb *fcb)
+{
+    handler->log_type = LOG_TYPE_STORAGE;
+    handler->log_read = log_fcb_read;
+    handler->log_append = log_fcb_append;
+    handler->log_walk = log_fcb_walk;
+    handler->log_flush = log_fcb_flush;
+    handler->log_arg = fcb;
+
+    return 0;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f2885ea6/sys/log/src/test/log_test.c
----------------------------------------------------------------------
diff --git a/sys/log/src/test/log_test.c b/sys/log/src/test/log_test.c
new file mode 100644
index 0000000..04135b4
--- /dev/null
+++ b/sys/log/src/test/log_test.c
@@ -0,0 +1,162 @@
+/**
+ * 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 <os/os.h>
+#include <testutil/testutil.h>
+#include <fcb/fcb.h>
+#include "log/log.h"
+
+static struct flash_area fcb_areas[] = {
+    [0] = {
+        .fa_off = 0x00000000,
+        .fa_size = 16 * 1024
+    },
+    [1] = {
+        .fa_off = 0x00004000,
+        .fa_size = 16 * 1024
+    }
+};
+static struct log_handler log_fcb_handler;
+static struct fcb log_fcb;
+static struct log my_log;
+
+static char *str_logs[] = {
+    "testdata",
+    "1testdata2",
+    NULL
+};
+static int str_idx = 0;
+static int str_max_idx = 0;
+
+TEST_CASE(log_setup_fcb)
+{
+    int rc;
+    int i;
+
+    log_fcb.f_sectors = fcb_areas;
+    log_fcb.f_sector_cnt = sizeof(fcb_areas) / sizeof(fcb_areas[0]);
+    log_fcb.f_magic = 0x7EADBADF;
+    log_fcb.f_version = 0;
+
+    for (i = 0; i < log_fcb.f_sector_cnt; i++) {
+        rc = flash_area_erase(&fcb_areas[i], 0, fcb_areas[i].fa_size);
+        TEST_ASSERT(rc == 0);
+    }
+    rc = fcb_init(&log_fcb);
+    TEST_ASSERT(rc == 0);
+    rc = log_fcb_handler_init(&log_fcb_handler, &log_fcb);
+    TEST_ASSERT(rc == 0);
+
+    log_register("log", &my_log, &log_fcb_handler);
+}
+
+TEST_CASE(log_append_fcb)
+{
+    char *str;
+
+    while (1) {
+        str = str_logs[str_max_idx];
+        if (!str) {
+            break;
+        }
+        log_printf(&my_log, 0, 0, str, strlen(str));
+        str_max_idx++;
+    }
+}
+
+static int
+log_test_walk1(struct log *log, void *arg, void *dptr, uint16_t len)
+{
+    int rc;
+    struct log_entry_hdr ueh;
+    char data[128];
+    int dlen;
+
+    TEST_ASSERT(str_idx < str_max_idx);
+
+    rc = log_read(log, dptr, &ueh, 0, sizeof(ueh));
+    TEST_ASSERT(rc == sizeof(ueh));
+
+    dlen = len - sizeof(ueh);
+    TEST_ASSERT(dlen < sizeof(data));
+
+    rc = log_read(log, dptr, data, sizeof(ueh), dlen);
+    TEST_ASSERT(rc == dlen);
+
+    data[rc] = '\0';
+
+    TEST_ASSERT(strlen(str_logs[str_idx]) == dlen);
+    TEST_ASSERT(!memcmp(str_logs[str_idx], data, dlen));
+    str_idx++;
+
+    return 0;
+}
+
+TEST_CASE(log_walk_fcb)
+{
+    int rc;
+
+    str_idx = 0;
+
+    rc = log_walk(&my_log, log_test_walk1, NULL);
+    TEST_ASSERT(rc == 0);
+}
+
+static int
+log_test_walk2(struct log *log, void *arg, void *dptr, uint16_t len)
+{
+    TEST_ASSERT(0);
+    return 0;
+}
+
+TEST_CASE(log_flush_fcb)
+{
+    int rc;
+
+    rc = log_flush(&my_log);
+    TEST_ASSERT(rc == 0);
+
+    rc = log_walk(&my_log, log_test_walk2, NULL);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_SUITE(log_test_all)
+{
+    log_setup_fcb();
+    log_append_fcb();
+    log_walk_fcb();
+    log_flush_fcb();
+}
+
+#ifdef MYNEWT_SELFTEST
+
+int
+main(int argc, char **argv)
+{
+    tu_config.tc_print_results = 1;
+    tu_init();
+
+    log_init();
+    log_test_all();
+
+    return tu_any_failed;
+}
+
+#endif