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:14 UTC

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

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