Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 68ed333dc -> d5a15930b


MYNEWT-568 log - Pass log_offset, not void*.

All the log functions required the void *arg parameter to point to an
instance of struct encode_off.  This commit:
    * Renames encode_off to log_offset
    * Makes this requirement explicit; functions take a struct
      log_offset* now, rather than void*.

The log_offset struct contains a void *lo_arg member, so there is still
a side-channel for handler-specifid data.


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

Branch: refs/heads/develop
Commit: d5a15930b0efb65aa033d3f21226ddd28dce0e67
Parents: e939080
Author: Christopher Collins <[email protected]>
Authored: Thu Jan 26 17:31:17 2017 -0800
Committer: Christopher Collins <[email protected]>
Committed: Thu Jan 26 17:51:43 2017 -0800

----------------------------------------------------------------------
 sys/log/full/include/log/log.h                  | 38 ++++++++++++--------
 sys/log/full/src/log.c                          | 24 ++++++-------
 sys/log/full/src/log_cbmem.c                    | 10 +++---
 sys/log/full/src/log_console.c                  |  3 +-
 sys/log/full/src/log_fcb.c                      | 10 +++---
 sys/log/full/src/log_nmgr.c                     | 36 +++++++++----------
 sys/log/full/src/log_shell.c                    |  3 +-
 sys/log/full/test/src/log_test.c                |  6 ++--
 sys/log/full/test/src/log_test.h                |  8 +++--
 sys/log/full/test/src/testcases/log_flush_fcb.c |  4 +--
 sys/log/full/test/src/testcases/log_walk_fcb.c  |  4 +--
 11 files changed, 81 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d5a15930/sys/log/full/include/log/log.h
----------------------------------------------------------------------
diff --git a/sys/log/full/include/log/log.h b/sys/log/full/include/log/log.h
index a55fa99..9874784 100644
--- a/sys/log/full/include/log/log.h
+++ b/sys/log/full/include/log/log.h
@@ -41,14 +41,34 @@ extern struct log_info g_log_info;
 
 struct log;
 
-typedef int (*log_walk_func_t)(struct log *, void *arg, void *offset,
-        uint16_t len);
+/**
+ * Used for walks and reads; indicates part of log to access.
+ */
+struct log_offset {
+    /* If   lo_ts == -1: Only access last log entry;
+     * Elif lo_ts == 0:  Don't filter by timestamp;
+     * Else:             Only access entries whose ts >= lo_ts.
+     */
+    int64_t lo_ts;
+
+    /* Only access entries whose index >= lo_index. */
+    uint32_t lo_index;
+
+    /* On read, lo_data_len gets populated with the number of bytes read. */
+    uint32_t lo_data_len;
+
+    /* Specific to walk / read function. */
+    void *lo_arg;
+};
+
+typedef int (*log_walk_func_t)(struct log *, struct log_offset *log_offset,
+        void *offset, uint16_t len);
 
 typedef int (*lh_read_func_t)(struct log *, void *dptr, void *buf,
         uint16_t offset, uint16_t len);
 typedef int (*lh_append_func_t)(struct log *, void *buf, int len);
 typedef int (*lh_walk_func_t)(struct log *,
-        log_walk_func_t walk_func, void *arg);
+        log_walk_func_t walk_func, struct log_offset *log_offset);
 typedef int (*lh_flush_func_t)(struct log *);
 /*
  * This function pointer points to a function that restores the numebr
@@ -77,16 +97,6 @@ struct log_entry_hdr {
 }__attribute__((__packed__));
 #define LOG_ENTRY_HDR_SIZE (sizeof(struct log_entry_hdr))
 
-/*
- * Encode request - packages log entry request and response
- */
-struct encode_off {
-    void *eo_arg; /* typecast CborEncoder */
-    int64_t eo_ts;
-    uint32_t eo_index;
-    uint32_t rsp_len;
-};
-
 #define LOG_LEVEL_DEBUG    (0)
 #define LOG_LEVEL_INFO     (1)
 #define LOG_LEVEL_WARN     (2)
@@ -211,7 +221,7 @@ void log_printf(struct log *log, uint16_t, uint16_t, char 
*, ...);
 int log_read(struct log *log, void *dptr, void *buf, uint16_t off,
         uint16_t len);
 int log_walk(struct log *log, log_walk_func_t walk_func,
-        void *arg);
+        struct log_offset *log_offset);
 int log_flush(struct log *log);
 int log_rtr_erase(struct log *log, void *arg);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d5a15930/sys/log/full/src/log.c
----------------------------------------------------------------------
diff --git a/sys/log/full/src/log.c b/sys/log/full/src/log.c
index cc4c56a..66b0fca 100644
--- a/sys/log/full/src/log.c
+++ b/sys/log/full/src/log.c
@@ -107,14 +107,13 @@ log_registered(struct log *log)
 }
 
 static int
-log_read_hdr_walk(struct log *log, void *arg, void *dptr, uint16_t len)
+log_read_hdr_walk(struct log *log, struct log_offset *log_offset, void *dptr,
+                  uint16_t len)
 {
-    struct encode_off *encode_off;
     struct log_entry_hdr *hdr;
     int rc;
 
-    encode_off = arg;
-    hdr = encode_off->eo_arg;
+    hdr = log_offset->lo_arg;
 
     rc = log_read(log, dptr, hdr, 0, sizeof *hdr);
     if (rc < sizeof *hdr) {
@@ -135,15 +134,15 @@ log_read_hdr_walk(struct log *log, void *arg, void *dptr, 
uint16_t len)
 static int
 log_read_last_hdr(struct log *log, struct log_entry_hdr *out_hdr)
 {
-    struct encode_off encode_off;
+    struct log_offset log_offset;
     int rc;
 
-    encode_off.eo_arg = out_hdr;
-    encode_off.eo_ts = -1;
-    encode_off.eo_index = 0;
-    encode_off.rsp_len = 0;
+    log_offset.lo_arg = out_hdr;
+    log_offset.lo_ts = -1;
+    log_offset.lo_index = 0;
+    log_offset.lo_data_len = 0;
 
-    rc = log_walk(log, log_read_hdr_walk, &encode_off);
+    rc = log_walk(log, log_read_hdr_walk, &log_offset);
     return rc;
 }
 
@@ -260,11 +259,12 @@ log_printf(struct log *log, uint16_t module, uint16_t 
level, char *msg, ...)
 }
 
 int
-log_walk(struct log *log, log_walk_func_t walk_func, void *arg)
+log_walk(struct log *log, log_walk_func_t walk_func,
+         struct log_offset *log_offset)
 {
     int rc;
 
-    rc = log->l_log->log_walk(log, walk_func, arg);
+    rc = log->l_log->log_walk(log, walk_func, log_offset);
     if (rc != 0) {
         goto err;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d5a15930/sys/log/full/src/log_cbmem.c
----------------------------------------------------------------------
diff --git a/sys/log/full/src/log_cbmem.c b/sys/log/full/src/log_cbmem.c
index e512895..b290e6c 100644
--- a/sys/log/full/src/log_cbmem.c
+++ b/sys/log/full/src/log_cbmem.c
@@ -55,12 +55,12 @@ log_cbmem_read(struct log *log, void *dptr, void *buf, 
uint16_t offset,
 }
 
 static int
-log_cbmem_walk(struct log *log, log_walk_func_t walk_func, void *arg)
+log_cbmem_walk(struct log *log, log_walk_func_t walk_func,
+               struct log_offset *log_offset)
 {
     struct cbmem *cbmem;
     struct cbmem_entry_hdr *hdr;
     struct cbmem_iter iter;
-    struct encode_off *encode_off = (struct encode_off *)arg;
     int rc;
 
     cbmem = (struct cbmem *) log->l_arg;
@@ -73,9 +73,9 @@ log_cbmem_walk(struct log *log, log_walk_func_t walk_func, 
void *arg)
     /*
      * if timestamp for request is < 1, return last log entry
      */
-    if (encode_off->eo_ts < 0) {
+    if (log_offset->lo_ts < 0) {
         hdr = cbmem->c_entry_end;
-        rc = walk_func(log, arg, (void *)hdr, hdr->ceh_len);
+        rc = walk_func(log, log_offset, (void *)hdr, hdr->ceh_len);
     } else {
         cbmem_iter_start(cbmem, &iter);
         while (1) {
@@ -84,7 +84,7 @@ log_cbmem_walk(struct log *log, log_walk_func_t walk_func, 
void *arg)
                 break;
             }
 
-            rc = walk_func(log, arg, (void *)hdr, hdr->ceh_len);
+            rc = walk_func(log, log_offset, (void *)hdr, hdr->ceh_len);
             if (rc == 1) {
                 break;
             }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d5a15930/sys/log/full/src/log_console.c
----------------------------------------------------------------------
diff --git a/sys/log/full/src/log_console.c b/sys/log/full/src/log_console.c
index e9e5556..0866a5a 100644
--- a/sys/log/full/src/log_console.c
+++ b/sys/log/full/src/log_console.c
@@ -52,7 +52,8 @@ log_console_read(struct log *log, void *dptr, void *buf, 
uint16_t offset,
 }
 
 static int
-log_console_walk(struct log *log, log_walk_func_t walk_func, void *arg)
+log_console_walk(struct log *log, log_walk_func_t walk_func,
+        struct log_offset *log_offset)
 {
     /* You don't walk console, console walk you. */
     return (OS_EINVAL);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d5a15930/sys/log/full/src/log_fcb.c
----------------------------------------------------------------------
diff --git a/sys/log/full/src/log_fcb.c b/sys/log/full/src/log_fcb.c
index 3353e41..ea104d4 100644
--- a/sys/log/full/src/log_fcb.c
+++ b/sys/log/full/src/log_fcb.c
@@ -99,12 +99,12 @@ log_fcb_read(struct log *log, void *dptr, void *buf, 
uint16_t offset,
 }
 
 static int
-log_fcb_walk(struct log *log, log_walk_func_t walk_func, void *arg)
+log_fcb_walk(struct log *log, log_walk_func_t walk_func,
+             struct log_offset *log_offset)
 {
     struct fcb *fcb;
     struct fcb_entry loc;
     struct fcb_entry *locp;
-    struct encode_off *encode_off = (struct encode_off *)arg;
     int rc;
 
     rc = 0;
@@ -115,12 +115,12 @@ log_fcb_walk(struct log *log, log_walk_func_t walk_func, 
void *arg)
     /*
      * if timestamp for request is < 0, return last log entry
      */
-    if (encode_off->eo_ts < 0) {
+    if (log_offset->lo_ts < 0) {
         locp = &fcb->f_active;
-        rc = walk_func(log, arg, (void *)locp, locp->fe_data_len);
+        rc = walk_func(log, log_offset, (void *)locp, locp->fe_data_len);
     } else {
         while (fcb_getnext(fcb, &loc) == 0) {
-            rc = walk_func(log, arg, (void *) &loc, loc.fe_data_len);
+            rc = walk_func(log, log_offset, (void *) &loc, loc.fe_data_len);
             if (rc) {
                 break;
             }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d5a15930/sys/log/full/src/log_nmgr.c
----------------------------------------------------------------------
diff --git a/sys/log/full/src/log_nmgr.c b/sys/log/full/src/log_nmgr.c
index bb45722..6d51cb6 100644
--- a/sys/log/full/src/log_nmgr.c
+++ b/sys/log/full/src/log_nmgr.c
@@ -55,20 +55,20 @@ static struct mgmt_handler log_nmgr_group_handlers[] = {
 
 /**
  * Log encode entry
- * @param log structure, arg:struct passed locally, dataptr, len
+ * @param log structure, log_offset, dataptr, len
  * @return 0 on success; non-zero on failure
  */
 static int
-log_nmgr_encode_entry(struct log *log, void *arg, void *dptr, uint16_t len)
+log_nmgr_encode_entry(struct log *log, struct log_offset *log_offset,
+                      void *dptr, uint16_t len)
 {
-    struct encode_off *encode_off = (struct encode_off *)arg;
     struct log_entry_hdr ueh;
     char data[128];
     int dlen;
     int rc;
     int rsp_len;
     CborError g_err = CborNoError;
-    CborEncoder *penc = (CborEncoder*)encode_off->eo_arg;
+    CborEncoder *penc = (CborEncoder*)log_offset->lo_arg;
     CborEncoder rsp;
     struct CborCntWriter cnt_writer;
     CborEncoder cnt_encoder;
@@ -90,13 +90,13 @@ log_nmgr_encode_entry(struct log *log, void *arg, void 
*dptr, uint16_t len)
      *      index >= specified index
      */
 
-    if (encode_off->eo_ts == 0) {
-        if (encode_off->eo_index > ueh.ue_index) {
+    if (log_offset->lo_ts == 0) {
+        if (log_offset->lo_index > ueh.ue_index) {
             goto err;
         }
-    } else if (ueh.ue_ts < encode_off->eo_ts   ||
-               (ueh.ue_ts == encode_off->eo_ts &&
-                ueh.ue_index < encode_off->eo_index)) {
+    } else if (ueh.ue_ts < log_offset->lo_ts   ||
+               (ueh.ue_ts == log_offset->lo_ts &&
+                ueh.ue_index < log_offset->lo_index)) {
         goto err;
     }
 
@@ -127,13 +127,13 @@ log_nmgr_encode_entry(struct log *log, void *arg, void 
*dptr, uint16_t len)
     g_err |= cbor_encode_text_stringz(&rsp, "module");
     g_err |= cbor_encode_uint(&rsp,  ueh.ue_module);
     g_err |= cbor_encoder_close_container(&cnt_encoder, &rsp);
-    rsp_len = encode_off->rsp_len;
+    rsp_len = log_offset->lo_data_len;
     rsp_len += cbor_encode_bytes_written(&cnt_encoder);
     if (rsp_len > 400) {
         rc = OS_ENOMEM;
         goto err;
     }
-    encode_off->rsp_len = rsp_len;
+    log_offset->lo_data_len = rsp_len;
 
     g_err |= cbor_encoder_create_map(penc, &rsp, CborIndefiniteLength);
     g_err |= cbor_encode_text_stringz(&rsp, "msg");
@@ -166,14 +166,14 @@ log_encode_entries(struct log *log, CborEncoder *cb,
                    int64_t ts, uint32_t index)
 {
     int rc;
-    struct encode_off encode_off;
+    struct log_offset log_offset;
     int rsp_len = 0;
     CborEncoder entries;
     CborError g_err = CborNoError;
     struct CborCntWriter cnt_writer;
     CborEncoder cnt_encoder;
 
-    memset(&encode_off, 0, sizeof(encode_off));
+    memset(&log_offset, 0, sizeof(log_offset));
 
     /* this code counts how long the message would be if we encoded
      * this outer structure using cbor. */
@@ -193,12 +193,12 @@ log_encode_entries(struct log *log, CborEncoder *cb,
     g_err |= cbor_encode_text_stringz(cb, "entries");
     g_err |= cbor_encoder_create_array(cb, &entries, CborIndefiniteLength);
 
-    encode_off.eo_arg  = (void*)&entries;
-    encode_off.eo_index    = index;
-    encode_off.eo_ts       = ts;
-    encode_off.rsp_len = rsp_len;
+    log_offset.lo_arg       = &entries;
+    log_offset.lo_index     = index;
+    log_offset.lo_ts        = ts;
+    log_offset.lo_data_len  = rsp_len;
 
-    rc = log_walk(log, log_nmgr_encode_entry, &encode_off);
+    rc = log_walk(log, log_nmgr_encode_entry, &log_offset);
 
     g_err |= cbor_encoder_close_container(cb, &entries);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d5a15930/sys/log/full/src/log_shell.c
----------------------------------------------------------------------
diff --git a/sys/log/full/src/log_shell.c b/sys/log/full/src/log_shell.c
index c56e53c..e0f6d71 100644
--- a/sys/log/full/src/log_shell.c
+++ b/sys/log/full/src/log_shell.c
@@ -35,7 +35,8 @@
 #include "console/console.h"
 
 static int
-shell_log_dump_entry(struct log *log, void *arg, void *dptr, uint16_t len)
+shell_log_dump_entry(struct log *log, struct log_offset *log_offset,
+                     void *dptr, uint16_t len)
 {
     struct log_entry_hdr ueh;
     char data[128];

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d5a15930/sys/log/full/test/src/log_test.c
----------------------------------------------------------------------
diff --git a/sys/log/full/test/src/log_test.c b/sys/log/full/test/src/log_test.c
index 63fa032..70be8f7 100644
--- a/sys/log/full/test/src/log_test.c
+++ b/sys/log/full/test/src/log_test.c
@@ -46,7 +46,8 @@ int str_idx = 0;
 int str_max_idx = 0;
 
 int
-log_test_walk1(struct log *log, void *arg, void *dptr, uint16_t len)
+log_test_walk1(struct log *log, struct log_offset *log_offset,
+               void *dptr, uint16_t len)
 {
     int rc;
     struct log_entry_hdr ueh;
@@ -74,7 +75,8 @@ log_test_walk1(struct log *log, void *arg, void *dptr, 
uint16_t len)
 }
 
 int
-log_test_walk2(struct log *log, void *arg, void *dptr, uint16_t len)
+log_test_walk2(struct log *log, struct log_offset *log_offset,
+               void *dptr, uint16_t len)
 {
     TEST_ASSERT(0);
     return 0;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d5a15930/sys/log/full/test/src/log_test.h
----------------------------------------------------------------------
diff --git a/sys/log/full/test/src/log_test.h b/sys/log/full/test/src/log_test.h
index c4e37a6..9b7a5e0 100644
--- a/sys/log/full/test/src/log_test.h
+++ b/sys/log/full/test/src/log_test.h
@@ -27,7 +27,7 @@
 #include "log/log.h"
 
 #ifdef __cplusplus
-#extern "C" {
+extern "C" {
 #endif
 
 #define FCB_FLASH_AREAS 2
@@ -44,8 +44,10 @@ extern char *str_logs[FCB_STR_LOGS_CNT];
 extern int str_idx;
 extern int str_max_idx;
 
-int log_test_walk1(struct log *log, void *arg, void *dptr, uint16_t len);
-int log_test_walk2(struct log *log, void *arg, void *dptr, uint16_t len);
+int log_test_walk1(struct log *log, struct log_offset *log_offset,
+                   void *dptr, uint16_t len);
+int log_test_walk2(struct log *log, struct log_offset *log_offset,
+                   void *dptr, uint16_t len);
 
 #ifdef __cplusplus
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d5a15930/sys/log/full/test/src/testcases/log_flush_fcb.c
----------------------------------------------------------------------
diff --git a/sys/log/full/test/src/testcases/log_flush_fcb.c 
b/sys/log/full/test/src/testcases/log_flush_fcb.c
index e799fd2..ce4e3c1 100644
--- a/sys/log/full/test/src/testcases/log_flush_fcb.c
+++ b/sys/log/full/test/src/testcases/log_flush_fcb.c
@@ -20,12 +20,12 @@
 
 TEST_CASE(log_flush_fcb)
 {
-    struct encode_off encode_off = { 0 };
+    struct log_offset log_offset = { 0 };
     int rc;
 
     rc = log_flush(&my_log);
     TEST_ASSERT(rc == 0);
 
-    rc = log_walk(&my_log, log_test_walk2, &encode_off);
+    rc = log_walk(&my_log, log_test_walk2, &log_offset);
     TEST_ASSERT(rc == 0);
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d5a15930/sys/log/full/test/src/testcases/log_walk_fcb.c
----------------------------------------------------------------------
diff --git a/sys/log/full/test/src/testcases/log_walk_fcb.c 
b/sys/log/full/test/src/testcases/log_walk_fcb.c
index e8a732c..0a8f9d5 100644
--- a/sys/log/full/test/src/testcases/log_walk_fcb.c
+++ b/sys/log/full/test/src/testcases/log_walk_fcb.c
@@ -20,11 +20,11 @@
 
 TEST_CASE(log_walk_fcb)
 {
-    struct encode_off encode_off = { 0 };
+    struct log_offset log_offset = { 0 };
     int rc;
 
     str_idx = 0;
 
-    rc = log_walk(&my_log, log_test_walk1, &encode_off);
+    rc = log_walk(&my_log, log_test_walk1, &log_offset);
     TEST_ASSERT(rc == 0);
 }

Reply via email to