This is an automated email from the ASF dual-hosted git repository.

vipulrahane pushed a commit to branch vipul/logging_num_entries_callbacks
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit 7387b63fd3fe068d0d1e6b5186b9ae638533a11a
Author: Vipul Rahane <vipul.rah...@juul.com>
AuthorDate: Fri Feb 7 21:19:27 2025 -0800

    Add further callbacks support
---
 sys/log/common/include/log_common/log_common.h |  47 +++-
 sys/log/full/include/log/log.h                 | 140 +++-------
 sys/log/full/src/log.c                         | 349 +++----------------------
 sys/log/full/src/log_cbmem.c                   |   2 +-
 sys/log/full/src/log_fcb.c                     |   9 +-
 sys/log/full/src/log_fcb2.c                    |  14 +-
 sys/log/full/src/log_shell.c                   |  46 ----
 sys/log/full/syscfg.yml                        |   5 +
 8 files changed, 123 insertions(+), 489 deletions(-)

diff --git a/sys/log/common/include/log_common/log_common.h 
b/sys/log/common/include/log_common/log_common.h
index 1ec4de551..45e259f8c 100644
--- a/sys/log/common/include/log_common/log_common.h
+++ b/sys/log/common/include/log_common/log_common.h
@@ -138,18 +138,6 @@ typedef void log_append_cb(struct log *log, uint32_t idx);
  */
 typedef void log_notify_rotate_cb(const struct log *log);
 
-/** @typedef log_mbuf_trailer_append_cb
- * @brief Callback that is executed each time the corresponding log entry is
- * appended to
- *
- * @param log                   The log that was just appended to
- * @param om                    Pointer to the mbuf that contains the log entry
- * @param loc                   Argument pointing to the location of
- *                              the entry
- * @return                      0 on success, non-zero on failure
- */
-typedef int log_trailer_mbuf_append_cb(struct log *log, struct os_mbuf *om, 
void *loc);
-
 /** @typedef log_cbmem_trailer_append_cb
  * @brief Callback that is executed each time the corresponding log entry is
  * appended to
@@ -182,10 +170,43 @@ typedef int log_cbmem_trailer_mbuf_append_cb(struct log 
*log, struct os_mbuf *om
  * @param buflen                Length of the trailer to be filled up
  * @param loc                   Argument pointing to the location of
  *                              the entry
+ * @param f_offset              Pointer to the offset(optional) at which 
append should
+ *                              happen
+ *
  * @return                      0 on success, non-zero on failure
  */
 typedef int log_trailer_append_cb(struct log *log, uint8_t *buf,
-                                  uint16_t buflen, void *loc);
+                                  uint16_t buflen, void *loc,
+                                  uint16_t *f_offset);
+
+/** @typedef log_mbuf_trailer_append_cb
+ * @brief Callback that is executed each time the corresponding log entry is
+ * appended to
+ *
+ * @param log                   The log that was just appended to
+ * @param om                    Pointer to the mbuf that contains the log entry
+ * @param loc                   Argument pointing to the location of
+ *                              the entry
+ * @param f_offset              The offset(optional) at which append should
+ *                              happen
+ *
+ * @return                      0 on success, non-zero on failure
+ */
+typedef int log_trailer_mbuf_append_cb(struct log *log, struct os_mbuf *om,
+                                       void *loc, uint16_t f_offset);
+
+/** @typedef log_process_trailer_cb
+ * @brief Callback that is executed each time a trailer is processed
+ *
+ * @param log                   The log that was just appended to
+ * @param hdr                   Pointer to the log netry header
+ * @param dptr                  Pointer to the data buffer
+ * @param len                   Length of the trailer
+ *
+ * @return                      0 on success, non-zero on failure
+ */
+typedef int log_process_trailer_cb(struct log *log, struct log_entry_hdr *hdr,
+                                   const void *dptr, uint16_t len);
 
 /** @typedef log_trailer_len_cb
  * @brief Callback used to read length of trailer in a log entry
diff --git a/sys/log/full/include/log/log.h b/sys/log/full/include/log/log.h
index 644fc6bea..91cb31310 100644
--- a/sys/log/full/include/log/log.h
+++ b/sys/log/full/include/log/log.h
@@ -140,9 +140,6 @@ struct log_handler {
 #define LOG_FLAGS_IMG_HASH    (1 << 0)
 #define LOG_FLAGS_TRAILER_SUPPORT (1 << 1)
 
-#define LOGS_TLV_NUM_ENTRIES   (1 << 0)
-#define LOG_TLV_NUM_TLVS      (1 << 1)
-
 #if MYNEWT_VAL(LOG_VERSION) == 3
 struct log_entry_hdr {
     int64_t ue_ts;
@@ -152,29 +149,17 @@ struct log_entry_hdr {
     uint8_t ue_etype : 4;
     uint8_t ue_flags : 4;
     uint8_t ue_imghash[4];
-    /* Number of entries field which helps in calculating number of
-     * entries per log, these go on incrementing similar to an index
-     * but per log.
-     */
-    uint32_t ue_num_entries;
-} __attribute__((__packed__));
-
-struct log_tlv {
-    uint8_t len;
-    uint8_t tag;
-    /* Value is of variable size appended based on len,
-     * val is logged after the tag and len are logged
-     */
 } __attribute__((__packed__));
-
 #else
 #error "Unsupported log version"
 #endif
 
 #define LOG_BASE_ENTRY_HDR_SIZE (15)
 
-#define LOG_NUM_ENTRIES_SIZE (sizeof(((struct log *)0)->l_num_entries))
-#define LOG_NUM_TLVS_SIZE    (1)
+/* Assume the flash alignment requirement is no stricter than 32. */
+#define LOG_FCB_MAX_ALIGN    32
+#define LOG_FCB2_MAX_ALIGN   32
+
 
 #if MYNEWT_VAL(LOG_FCB2)
 #define LF_MAX_ALIGN LOG_FCB2_MAX_ALIGN
@@ -182,22 +167,16 @@ struct log_tlv {
 #define LF_MAX_ALIGN LOG_FCB_MAX_ALIGN
 #endif
 
-#define LOG_FCB_MAX_TLV_SIZE(__tlv_name__) \
-    /* sizeof(struct log_tlv)) + alignment */ \
-    (LF_MAX_ALIGN + \
-     /* Max size per value of TLV including alignment */ \
-     (LOG_ ## __tlv_name__ ## _SIZE/LF_MAX_ALIGN) ? \
-     (LOG_ ## __tlv_name__ ## _SIZE + LF_MAX_ALIGN) : \
-     LF_MAX_ALIGN)
-
-#define LOG_FCB_MAX_TLVS_SIZE LOG_FCB_MAX_TLV_SIZE(NUM_ENTRIES) + \
-    LOG_FCB_MAX_TLV_SIZE(NUM_TLVS)
-
 #define LOG_FCB_EXT_HDR_SIZE LOG_BASE_ENTRY_HDR_SIZE + LOG_IMG_HASHLEN + \
     LF_MAX_ALIGN
 
-#define LOG_FCB_FLAT_BUF_SIZE (LOG_FCB_EXT_HDR_SIZE > LOG_FCB_MAX_TLVS_SIZE) ? 
\
-    LOG_FCB_EXT_HDR_SIZE : LOG_FCB_MAX_TLVS_SIZE
+#ifndef LOG_FCB_FLAT_BUF_SIZE
+/* Assuming the trailer fits in this, an arbitrary value */
+#define LOG_FCB_FLAT_BUF_SIZE (LOG_FCB_EXT_HDR_SIZE > LF_MAX_ALIGN * 3) ? \
+    LOG_FCB_EXT_HDR_SIZE : LF_MAX_ALIGN * 3
+#endif
+
+#define LOG_MAX_TRAILER_LEN    MYNEWT_VAL(LOG_MAX_TRAILER_LEN)
 
 #define LOG_MODULE_STR(module)      log_module_get_name(module)
 
@@ -264,13 +243,14 @@ struct log {
 #if !MYNEWT_VAL(LOG_GLOBAL_IDX)
     uint32_t l_idx;
 #endif
-    uint32_t l_num_entries;
 #if MYNEWT_VAL(LOG_FLAGS_TRAILER_SUPPORT)
     log_trailer_len_cb *l_trailer_len_cb;
     log_trailer_append_cb *l_trailer_append_cb;
+    log_process_trailer_cb *l_process_trailer_cb;
     log_trailer_mbuf_append_cb *l_trailer_mbuf_append_cb;
     log_cbmem_trailer_append_cb *l_cbmem_trailer_append_cb;
     log_cbmem_trailer_mbuf_append_cb *l_cbmem_trailer_mbuf_append_cb;
+    void *l_trailer_arg;
 #endif
 #if MYNEWT_VAL(LOG_STATS)
     STATS_SECT_DECL(logs) l_stats;
@@ -795,33 +775,6 @@ log_set_rotate_notify_cb(struct log *log, 
log_notify_rotate_cb *cb);
 int log_set_watermark(struct log *log, uint32_t index);
 #endif
 
-/**
- * Fill number of entries
- *
- * @param log Ptr to log structure
- * @param dptr Ptr to data to be read
- * @param num_entries Ptr to number of entries
- * @param offset Offset of the num of entries field in the log entry
- *
- * @return 0 on success, non-zero on failure
- */
-int log_fill_num_entries(struct log *log, const void *dptr,
-                         uint32_t *num_entries,
-                         uint16_t offset);
-/**
- * Fill number of tlvs
- *
- * @param log Ptr to log structure
- * @param dptr Ptr to data to be read
- * @param num_entries Ptr to number of entries
- * @param offset Offset of the num of entries field in the log entry
- *
- * @return 0 on success, non-zero on failure
- */
-int
-log_fill_num_tlvs(struct log *log, const void *dptr, uint8_t *num_tlvs,
-                  uint16_t offset);
-
 /**
  * Fill log current image hash
  *
@@ -832,29 +785,6 @@ log_fill_num_tlvs(struct log *log, const void *dptr, 
uint8_t *num_tlvs,
 int
 log_fill_current_img_hash(struct log_entry_hdr *hdr);
 
-/**
- * Reads the log entry's header from the specified log and log index
- *
- * @param log                   The log to read from.
- * @param idx                   Index of the log entry to read header from
- * @param out_hdr               On success, the last entry header gets written
- *                                  here.
- *
- * @return                      0 on success; nonzero on failure.
- */
-int
-log_read_hdr_by_idx(struct log *log, uint32_t idx, struct log_entry_hdr 
*out_hdr);
-
-/**
- * Get number of entries in log
- *
- * @param log The log to get number of entries for
- * @param idx The log index to read number of entries from
- * @param num_entries Ptr to fill up number of entries in log
- */
-int
-log_get_entries(struct log *log, uint32_t idx, uint32_t *entries);
-
 /**
  * Get the length of data in medium - storage (fcb/fcb2), memory or stream
  *
@@ -874,20 +804,6 @@ log_len_in_medium(struct log *log, uint16_t len);
  */
 uint16_t log_trailer_len(struct log *log, const struct log_entry_hdr *hdr);
 
-/**
- * @brief Reads a single log entry trailer.
- *
- * @param log                   The log to read from.
- * @param dptr                  Medium-specific data describing the area to
- *                                  read from; typically obtained by a call to
- *                                  `log_walk`.
- * @param tlv                   tlv type
- * @param buf                   Value buffer
- *
- * @return                      0 on success; nonzero on failure.
- */
-int log_read_trailer(struct log *log, const void *dptr, uint16_t tlv, void 
*buf);
-
 /**
  * Append trailer to the log entry
  *
@@ -895,10 +811,13 @@ int log_read_trailer(struct log *log, const void *dptr, 
uint16_t tlv, void *buf)
  * @param buf Pointer to the buffer containing trailer
  * @param buflen Length of the trailer
  * @param loc Pointer to the log entry
+ * @param f_offset Pointer to the offset(optional) at which append should
+ *                 happen, gets updated once append is successful
+ * @return 0 on success, non-zero on failure
  */
 int
 log_trailer_append(struct log *log, uint8_t *buf, uint16_t buflen,
-                   void *loc);
+                   void *loc, uint16_t *f_offset);
 
 /**
  * Append trailer in an mbuf to the log entry
@@ -906,9 +825,27 @@ log_trailer_append(struct log *log, uint8_t *buf, uint16_t 
buflen,
  * @param log Pointer to the log
  * @param om Pointer to the os_mbuf
  * @param loc Pointer to the log entry
+ * @param f_offset The offset(optional) at which append should
+ *                 happen
+ */
+int
+log_mbuf_trailer_append(struct log *log, struct os_mbuf *om, void *loc,
+                        uint16_t f_offset);
+
+/**
+ * Reads the final log entry's header and processes trailer if the flag
+ * indicates so from the specified log.
+ *
+ * @param log                   The log to read from.
+ * @param out_hdr               On success, the last entry header gets written
+ *                              here.
+ * @param trailer_exists        Pointer to a boolean
+ *
+ * @return                      0 on success; nonzero on failure.
  */
 int
-log_mbuf_trailer_append(struct log *log, struct os_mbuf *om, void *loc);
+log_read_last_hdr_trailer(struct log *log, struct log_entry_hdr *out_hdr,
+                          bool *trailer_exists);
 
 /**
  * Register trailer callbacks
@@ -916,13 +853,16 @@ log_mbuf_trailer_append(struct log *log, struct os_mbuf 
*om, void *loc);
  * @param log Pointer to the log
  * @param ltac Pointer to the log trailer append callback
  * @param ltlc Pointer to the log trailer length callback
+ * @param lptc Pointer to the log process trailer callback
  */
 static inline void
 log_register_trailer_cbs(struct log *log, log_trailer_append_cb *ltac,
-                         log_trailer_len_cb *ltlc)
+                         log_trailer_len_cb *ltlc,
+                         log_process_trailer_cb *lptc)
 {
     log->l_trailer_append_cb = ltac;
     log->l_trailer_len_cb = ltlc;
+    log->l_process_trailer_cb = lptc;
 }
 
 /**
diff --git a/sys/log/full/src/log.c b/sys/log/full/src/log.c
index 3fde2987f..a1eda0b3f 100644
--- a/sys/log/full/src/log.c
+++ b/sys/log/full/src/log.c
@@ -293,83 +293,20 @@ log_find(const char *name)
     return log;
 }
 
+/* Argument for header/trailer walks */
 struct log_read_hdr_arg {
     struct log_entry_hdr *hdr;
     int read_success;
+    bool trailer_exists;
+    void *arg_trailer;
 };
 
 static int
-log_update_num_entries_hdr_walk(struct log *log, struct log_offset *log_offset,
-                                const void *dptr, uint16_t len)
-{
-    uint32_t *num_entries;
-    uint16_t offset = 0;
-    int rc;
-    struct log_entry_hdr hdr;
-
-    (void)offset;
-    num_entries = log_offset->lo_arg;
-
-    rc = log_read(log, dptr, &hdr, 0, LOG_BASE_ENTRY_HDR_SIZE);
-    if (rc >= LOG_BASE_ENTRY_HDR_SIZE) {
-        *num_entries += 1;
-        return 0;
-    }
-
-    return -1;
-}
-
-static int
-log_process_tlvs(struct log *log, const void *dptr, uint8_t num_tlvs,
-                 uint16_t tlv_tag, void *buf, uint16_t len)
-{
-    struct log_tlv *tlv;
-    uint8_t tmpbuf[sizeof(*tlv) + 8] = {0};
-    uint16_t offset;
-    int tlv_len;
-    int rc = SYS_ENOENT;
-
-    offset = len;
-    tlv_len = log_len_in_medium(log, sizeof(*tlv));
-    do {
-        offset -= tlv_len;
-        rc = log_read(log, dptr, tmpbuf, offset, tlv_len);
-        if (rc < tlv_len) {
-            return SYS_EINVAL;
-        }
-        tlv = (struct log_tlv *)tmpbuf;
-
-        offset -= log_len_in_medium(log, tlv->len);
-        if (tlv->tag != tlv_tag) {
-            continue;
-        }
-
-        switch (tlv_tag) {
-        case LOGS_TLV_NUM_ENTRIES:
-            rc = log_fill_num_entries(log, dptr, buf, offset);
-            break;
-        case LOG_TLV_NUM_TLVS:
-            rc = log_fill_num_tlvs(log, dptr, buf, offset);
-            break;
-        default:
-            return SYS_ENOTSUP;
-        }
-
-        if (!rc) {
-            return SYS_EOK;
-        }
-    } while (offset && num_tlvs);
-
-    return SYS_EOK;
-}
-
-static int
-log_read_hdr_walk(struct log *log, struct log_offset *log_offset,
-                  const void *dptr, uint16_t len)
+log_read_hdr_trailer_walk(struct log *log, struct log_offset *log_offset,
+                          const void *dptr, uint16_t len)
 {
     struct log_read_hdr_arg *arg;
     int rc;
-    uint8_t num_tlvs = 0;
 
     arg = log_offset->lo_arg;
 
@@ -386,26 +323,13 @@ log_read_hdr_walk(struct log *log, struct log_offset 
*log_offset,
     }
 
     if (arg->hdr->ue_flags & LOG_FLAGS_TRAILER_SUPPORT) {
-        /* Read number of TLVs, if it does not exist
-         * num_tlvs = 0, so only one TLV will get read
-         * from the end of the entry
-         */
-        rc = log_process_tlvs(log, dptr, 0, LOG_TLV_NUM_TLVS,
-                              &num_tlvs, len);
-        if (!rc || rc == SYS_ENOTSUP) {
+        rc = log->l_process_trailer_cb(log, arg->arg_trailer, dptr, len);
+        if (!rc) {
             arg->read_success = 1;
+            arg->trailer_exists = true;
         } else {
             arg->read_success = 0;
-        }
-
-        if (!rc || rc == SYS_ENOTSUP) {
-            rc = log_process_tlvs(log, dptr, num_tlvs, LOGS_TLV_NUM_ENTRIES,
-                                  &arg->hdr->ue_num_entries, len);
-            if (!rc || rc == SYS_ENOTSUP) {
-                arg->read_success = 1;
-            } else {
-                arg->read_success = 0;
-            }
+            arg->trailer_exists = false;
         }
     }
 
@@ -414,119 +338,45 @@ log_read_hdr_walk(struct log *log, struct log_offset 
*log_offset,
 }
 
 /**
- * Update number of entries of the specified log.
- *
- * @param log                   The log to read from.
- * @param num_entries           Pointer to the number of entries.
- *
- * @return                      0 on success; nonzero on failure.
- */
-static int
-log_update_num_entries(struct log *log, uint32_t *num_entries)
-{
-    struct log_offset log_offset;
-    int rc = 0;
-
-
-    log_offset.lo_arg = num_entries;
-    log_offset.lo_ts = 0;
-    log_offset.lo_index = 0;
-    log_offset.lo_data_len = 0;
-
-    rc = log_walk(log, log_update_num_entries_hdr_walk, &log_offset);
-    if (rc) {
-        return -1;
-    }
-
-    return 0;
-}
-
-/**
- * Reads the final log entry's header from the specified log.
+ * Reads the final log entry's header and processes trailer if the flag
+ * indicates so from the specified log.
  *
  * @param log                   The log to read from.
  * @param out_hdr               On success, the last entry header gets written
- *                                  here.
+ *                              here.
+ * @param trailer_exists        Pointer to a boolean
  *
  * @return                      0 on success; nonzero on failure.
  */
-static int
-log_read_last_hdr(struct log *log, struct log_entry_hdr *out_hdr)
+int
+log_read_last_hdr_trailer(struct log *log, struct log_entry_hdr *out_hdr,
+                          bool *trailer_exists)
 {
     struct log_read_hdr_arg arg;
+    uint8_t trailer_arg[LOG_FCB_FLAT_BUF_SIZE];
     struct log_offset log_offset = {};
+    int sr;
 
     arg.hdr = out_hdr;
     arg.read_success = 0;
+    arg.trailer_exists = false;
+    arg.arg_trailer = trailer_arg;
 
     log_offset.lo_arg = &arg;
     log_offset.lo_ts = -1;
     log_offset.lo_index = 0;
     log_offset.lo_data_len = 0;
 
-    log_walk(log, log_read_hdr_walk, &log_offset);
+    log_walk(log, log_read_hdr_trailer_walk, &log_offset);
     if (!arg.read_success) {
         return -1;
     }
 
-    return 0;
-}
-
-/**
- * Get number of entries in log
- *
- * @param log The log to get number of entries for
- * @param idx The log index to read number of entries from
- * @param num_entries Ptr to fill up number of entries in log
- *
- * @return 0 on success, non-zero on failure
- */
-int
-log_get_entries(struct log *log, uint32_t idx, uint32_t *entries)
-{
-#if MYNEWT_VAL(LOG_FLAGS_TRAILER_SUPPORT) && MYNEWT_VAL(LOGS_TLV_NUM_ENTRIES)
-    int rc = 0;
-    struct log_entry_hdr hdr;
-
-    rc = log_read_hdr_by_idx(log, idx, &hdr);
-    if (!rc) {
-        *entries = log->l_num_entries - hdr.ue_num_entries;
-        return SYS_EOK;
-    } else {
-        return rc;
-    }
-#else
-    return SYS_ENOTSUP;
-#endif
-}
-
-/**
- * Reads the log entry's header from the specified log and log index
- *
- * @param log                   The log to read from.
- * @param idx                   Index of the log entry to read header from
- * @param out_hdr               On success, the last entry header gets written
- *                                  here.
- *
- * @return                      0 on success; nonzero on failure.
- */
-int
-log_read_hdr_by_idx(struct log *log, uint32_t idx, struct log_entry_hdr 
*out_hdr)
-{
-    struct log_read_hdr_arg arg;
-    struct log_offset log_offset;
-
-    arg.hdr = out_hdr;
-    arg.read_success = 0;
-
-    log_offset.lo_arg = &arg;
-    log_offset.lo_ts = 0;
-    log_offset.lo_index = idx;
-    log_offset.lo_data_len = 0;
-
-    log_walk(log, log_read_hdr_walk, &log_offset);
-    if (!arg.read_success) {
-        return -1;
+    if (trailer_exists) {
+        *trailer_exists = arg.trailer_exists;
+        OS_ENTER_CRITICAL(sr);
+        log->l_trailer_arg = arg.arg_trailer;
+        OS_EXIT_CRITICAL(sr);
     }
 
     return 0;
@@ -540,7 +390,6 @@ log_register(const char *name, struct log *log, const 
struct log_handler *lh,
              void *arg, uint8_t level)
 {
     struct log_entry_hdr hdr;
-    uint32_t num_entries = 0;
     int sr;
     int rc;
 
@@ -581,15 +430,8 @@ log_register(const char *name, struct log *log, const 
struct log_handler *lh,
 
     if (log->l_log->log_type == LOG_TYPE_STORAGE) {
         memset(&hdr, 0, sizeof(hdr));
-        rc = log_read_last_hdr(log, &hdr);
+        rc = log_read_last_hdr_trailer(log, &hdr, NULL);
         if (rc == 0) {
-            /* If the number of entries are not set in the last header,
-             * it was probably not supported when the entry was logged.
-             * Count number of entries in this specific case
-             */
-            if (!hdr.ue_num_entries) {
-                log_update_num_entries(log, &num_entries);
-            }
             /* If this is a persisted log, read the index from its most
              * recent entry. We need to ensure the index of all subsequently
              * written entries is monotonically increasing.
@@ -604,15 +446,6 @@ log_register(const char *name, struct log *log, const 
struct log_handler *lh,
                 log->l_idx = hdr.ue_index;
             }
 #endif
-            /* If this is a persisted log, read the num_entries from its most
-             * recent entry. We need to ensure the number of entries are
-             * monotonically increasing.
-             */
-            if (!hdr.ue_num_entries) {
-                log->l_num_entries = num_entries;
-            } else if (hdr.ue_num_entries >= log->l_num_entries) {
-                log->l_num_entries = hdr.ue_num_entries + 1;
-            }
             OS_EXIT_CRITICAL(sr);
         }
     }
@@ -661,20 +494,21 @@ log_trailer_len(struct log *log, const struct 
log_entry_hdr *hdr)
 
 int
 log_trailer_append(struct log *log, uint8_t *buf, uint16_t buflen,
-                   void *loc)
+                   void *loc, uint16_t *f_offset)
 {
     if (log->l_trailer_append_cb) {
-        return log->l_trailer_append_cb(log, buf, buflen, loc);
+        return log->l_trailer_append_cb(log, buf, buflen, loc, f_offset);
     } else {
         return SYS_ENOTSUP;
     }
 }
 
 int
-log_mbuf_trailer_append(struct log *log, struct os_mbuf *om, void *loc)
+log_mbuf_trailer_append(struct log *log, struct os_mbuf *om, void *loc,
+                        uint16_t f_offset)
 {
     if (log->l_trailer_mbuf_append_cb) {
-        return log->l_trailer_mbuf_append_cb(log, om, loc);
+        return log->l_trailer_mbuf_append_cb(log, om, loc, f_offset);
     } else {
         return SYS_ENOTSUP;
     }
@@ -805,9 +639,8 @@ log_append_prepare(struct log *log, uint8_t module, uint8_t 
level,
     }
 #endif
 
-#if MYNEWT_VAL(LOG_FLAGS_TRAILER_SUPPORT) && MYNEWT_VAL(LOGS_TLV_NUM_ENTRIES)
+#if MYNEWT_VAL(LOG_FLAGS_TRAILER_SUPPORT)
     ue->ue_flags |= LOG_FLAGS_TRAILER_SUPPORT;
-    ue->ue_num_entries = log->l_num_entries;
 #endif
 
 err:
@@ -1228,78 +1061,6 @@ log_read_hdr(struct log *log, const void *dptr, struct 
log_entry_hdr *hdr)
     return 0;
 }
 
-#if MYNEWT_VAL(LOG_FLAGS_TRAILER_SUPPORT)
-static int
-log_tlv_exists(uint16_t tlv)
-{
-    switch (tlv) {
-#if MYNEWT_VAL(LOGS_TLV_NUM_ENTRIES)
-    case LOGS_TLV_NUM_ENTRIES:
-        return 0;
-#endif
-
-#if MYNEWT_VAL(LOG_TLV_NUM_TLVS)
-    case LOG_TLV_NUM_TLVS:
-        return 0;
-#endif
-    default:
-        return SYS_ENOTSUP;
-    }
-}
-#endif
-
-int
-log_read_trailer(struct log *log, const void *dptr, uint16_t tlv, void *buf)
-{
-#if MYNEWT_VAL(LOG_FLAGS_TRAILER_SUPPORT)
-    int rc;
-    struct log_entry_hdr hdr;
-    uint16_t entry_len;
-    uint8_t num_tlvs = 0;
-
-    if (!buf) {
-        return SYS_EINVAL;
-    }
-
-    rc = log_tlv_exists(tlv);
-    if (rc) {
-        return rc;
-    }
-
-    rc = log_read_hdr(log, dptr, &hdr);
-    if (rc) {
-        return rc;
-    }
-
-    if (hdr.ue_flags & LOG_FLAGS_TRAILER_SUPPORT) {
-        entry_len = log_read_entry_len(log, dptr);
-        if (tlv == LOG_TLV_NUM_TLVS) {
-            rc = log_process_tlvs(log, dptr, 0, LOG_TLV_NUM_TLVS, &num_tlvs,
-                                  entry_len);
-            if (rc) {
-                return rc;
-            }
-
-            memcpy(buf, &num_tlvs, LOG_NUM_TLVS_SIZE);
-        }
-
-        if (tlv == LOGS_TLV_NUM_ENTRIES) {
-            rc = log_process_tlvs(log, dptr, num_tlvs, LOGS_TLV_NUM_ENTRIES,
-                                  &hdr.ue_num_entries, entry_len);
-            if (rc) {
-                return rc;
-            }
-
-            memcpy(buf, &hdr.ue_num_entries, LOG_NUM_ENTRIES_SIZE);
-        }
-    }
-
-    return 0;
-#else
-    return SYS_ENOTSUP;
-#endif
-}
-
 int
 log_read_body(struct log *log, const void *dptr, void *buf, uint16_t off,
               uint16_t len)
@@ -1350,7 +1111,7 @@ log_flush(struct log *log)
 {
     int rc;
 
-    log->l_num_entries = 0;
+    log->l_trailer_arg = (uintptr_t)0;
 
     rc = log->l_log->log_flush(log);
     if (rc != 0) {
@@ -1440,48 +1201,6 @@ log_set_max_entry_len(struct log *log, uint16_t 
max_entry_len)
     log->l_max_entry_len = max_entry_len;
 }
 
-int
-log_fill_num_entries(struct log *log, const void *dptr, uint32_t *num_entries,
-                     uint16_t offset)
-{
-#if MYNEWT_VAL(LOG_FLAGS_TRAILER_SUPPORT) && MYNEWT_VAL(LOGS_TLV_NUM_ENTRIES)
-    int rc = 0;
-    uint32_t tmpdata;
-
-    rc = log_read(log, dptr, &tmpdata, offset, LOG_NUM_ENTRIES_SIZE);
-    if (rc < LOG_NUM_ENTRIES_SIZE) {
-        return rc;
-    }
-
-    *num_entries = tmpdata;
-
-    return SYS_EOK;
-#else
-    return SYS_ENOTSUP;
-#endif
-}
-
-int
-log_fill_num_tlvs(struct log *log, const void *dptr, uint8_t *num_tlvs,
-                  uint16_t offset)
-{
-#if MYNEWT_VAL(LOG_FLAGS_TRAILER_SUPPORT) && MYNEWT_VAL(LOG_TLV_NUM_TLVS)
-    int rc = 0;
-    uint32_t tmpdata;
-
-    rc = log_read(log, dptr, &tmpdata, offset, LOG_NUM_TLVS_SIZE);
-    if (rc < LOG_NUM_TLVS_SIZE) {
-        return rc;
-    }
-
-    *num_tlvs = tmpdata;
-
-    return SYS_EOK;
-#else
-    return SYS_ENOTSUP;
-#endif
-}
-
 int
 log_fill_current_img_hash(struct log_entry_hdr *hdr)
 {
diff --git a/sys/log/full/src/log_cbmem.c b/sys/log/full/src/log_cbmem.c
index d43d79912..94a12b7b5 100644
--- a/sys/log/full/src/log_cbmem.c
+++ b/sys/log/full/src/log_cbmem.c
@@ -27,7 +27,7 @@ log_cbmem_append_body(struct log *log, const struct 
log_entry_hdr *hdr,
 {
     int rc = 0;
     struct cbmem *cbmem;
-    uint8_t trailer[LOG_NUM_ENTRIES_SIZE] = {0};
+    uint8_t trailer[LOG_MAX_TRAILER_LEN] = {0};
     uint16_t trailer_len = 0;
     struct cbmem_scat_gath *sg = (struct cbmem_scat_gath *)&trailer[0];
 
diff --git a/sys/log/full/src/log_fcb.c b/sys/log/full/src/log_fcb.c
index e25bcd70f..91ff20a97 100644
--- a/sys/log/full/src/log_fcb.c
+++ b/sys/log/full/src/log_fcb.c
@@ -28,9 +28,6 @@
 #include "log/log.h"
 #include <console/console.h>
 
-/* Assume the flash alignment requirement is no stricter than 32. */
-#define LOG_FCB_MAX_ALIGN   32
-
 static int log_fcb_rtr_erase(struct log *log);
 
 static int
@@ -425,7 +422,7 @@ log_fcb_append_body(struct log *log, const struct 
log_entry_hdr *hdr,
             /* The first trailer gets appended after the padding + 
trailer_alignment
              * Trailers start from updated loc.fe_data_off.
              */
-            rc = log_trailer_append(log, buf, trailer_len, &loc);
+            rc = log_trailer_append(log, buf, trailer_len, &loc, NULL);
             if (rc && rc != SYS_ENOTSUP) {
                 return rc;
             }
@@ -511,11 +508,11 @@ log_fcb_append_mbuf_body(struct log *log, const struct 
log_entry_hdr *hdr,
 
     if (hdr->ue_flags & LOG_FLAGS_TRAILER_SUPPORT) {
 #if MYNEWT_VAL(LOG_FLAGS_TRAILER_SUPPORT)
-        /* The first TLV gets appended after the padding + trailer_alignment
+        /* The trailer gets appended after the padding + trailer_alignment
          * Trailers start from updated loc.fe_data_off. Write everything
          * together
          */
-        rc = log_mbuf_trailer_append(log, om, &loc);
+        rc = log_mbuf_trailer_append(log, om, &loc, 0);
         if (rc && rc != SYS_ENOTSUP) {
             return rc;
         }
diff --git a/sys/log/full/src/log_fcb2.c b/sys/log/full/src/log_fcb2.c
index 3264752a2..f17246198 100644
--- a/sys/log/full/src/log_fcb2.c
+++ b/sys/log/full/src/log_fcb2.c
@@ -27,9 +27,6 @@
 #include "log/log.h"
 #include "fcb/fcb2.h"
 
-/* Assume the flash alignment requirement is no stricter than 32. */
-#define LOG_FCB2_MAX_ALIGN   32
-
 static int log_fcb2_rtr_erase(struct log *log);
 
 /**
@@ -326,11 +323,12 @@ log_fcb2_append_body(struct log *log, const struct 
log_entry_hdr *hdr,
             }
 
             chunk_sz += offset;
-            /* The first TLV gets appended after the padding + 
trailer_alignment
+
+            /* The first trailer gets appended after the padding + 
trailer_alignment
              * Trailers start from updated chunk_sz offset.
              */
-            rc = log_fcb2_tlvs_write(log, buf, sizeof(buf), &loc, &chunk_sz);
-            if (rc) {
+            rc = log_trailer_append(log, buf, trailer_len, &loc, &chunk_sz);
+            if (rc && rc != SYS_ENOTSUP) {
                 return rc;
             }
         }
@@ -415,11 +413,11 @@ log_fcb2_append_mbuf_body(struct log *log, const struct 
log_entry_hdr *hdr,
 
     if (hdr->ue_flags & LOG_FLAGS_TRAILER_SUPPORT) {
 #if MYNEWT_VAL(LOG_FLAGS_TRAILER_SUPPORT)
-        /* The first TLV gets appended after the padding + trailer_alignment
+        /* The trailer gets appended after the padding + trailer_alignment
          * Trailers start from updated loc.fe_data_off. Write everything
          * together
          */
-        rc = log_fcb2_mbuf_tlvs_write(log, om, &loc, len);
+        rc = log_mbuf_trailer_append(log, om, &loc, len);
         if (rc != 0) {
             return rc;
         }
diff --git a/sys/log/full/src/log_shell.c b/sys/log/full/src/log_shell.c
index 257233b2a..60d37399d 100644
--- a/sys/log/full/src/log_shell.c
+++ b/sys/log/full/src/log_shell.c
@@ -88,13 +88,6 @@ shell_log_dump_entry(struct log *log, struct log_offset 
*log_offset,
     bool read_hash = ueh->ue_flags & LOG_FLAGS_IMG_HASH;
     bool add_lf = true;
 
-#if MYNEWT_VAL(LOG_FLAGS_TRAILER_SUPPORT) && MYNEWT_VAL(LOGS_TLV_NUM_ENTRIES)
-    bool read_num_entries = ueh->ue_flags & LOG_FLAGS_TRAILER_SUPPORT;
-#else
-    bool read_num_entries = false;
-#endif
-    uint32_t entries = 0;
-
     dlen = min(len, 128);
 
     if (arg) {
@@ -106,16 +99,6 @@ shell_log_dump_entry(struct log *log, struct log_offset 
*log_offset,
     }
 
     if (read_data) {
-#if MYNEWT_VAL(LOG_FLAGS_TRAILER_SUPPORT)
-        dlen -= log_len_in_medium(log, sizeof(struct log_tlv));
-
-        rc = log_read_trailer(log, dptr, LOGS_TLV_NUM_ENTRIES, &entries);
-        if (!rc) {
-            dlen -= log_len_in_medium(log, LOG_NUM_ENTRIES_SIZE);
-        } else {
-            console_printf("failed to read trailer\n");
-        }
-#endif
         rc = log_read_body(log, dptr, data, 0, dlen);
         if (rc < 0) {
             return rc;
@@ -135,16 +118,6 @@ shell_log_dump_entry(struct log *log, struct log_offset 
*log_offset,
         console_printf(" [%llu] ", ueh->ue_ts);
     }
 
-    if (read_num_entries) {
-#if MYNEWT_VAL(LOG_FLAGS_TRAILER_SUPPORT)
-        dlen -= log_len_in_medium(log, sizeof(struct log_tlv));
-        rc = log_read_trailer(log, dptr, LOGS_TLV_NUM_ENTRIES, &entries);
-        if (!rc) {
-            dlen -= log_len_in_medium(log, LOG_NUM_ENTRIES_SIZE);
-        }
-#endif
-        console_printf("[ne=%u]", (unsigned int)entries);
-    }
     console_printf(" [%llu] ", ueh->ue_ts);
 #if MYNEWT_VAL(LOG_SHELL_SHOW_INDEX)
     console_printf(" [ix=%lu] ", ueh->ue_index);
@@ -201,8 +174,6 @@ shell_log_dump_cmd(int argc, char **argv)
     bool reverse = false;
     bool dump_logs = true;
     struct walk_arg arg = {};
-    bool num_entries = false;
-    uint32_t entries;
     int i;
     int rc;
 
@@ -246,16 +217,6 @@ shell_log_dump_cmd(int argc, char **argv)
         /* the -c option is to clear a log (or logs). */
         if (!strcmp(argv[i], "-c")) {
             clear_log = true;
-        } else if (argc == 3 && !strcmp(argv[i], "-ne")) {
-            num_entries = true;
-            log_name = argv[i+1];
-            break;
-        } else if (argc == 5 && !strcmp(argv[i], "-ne") &&
-                   !strcmp(argv[i+2], "-i")) {
-            num_entries = true;
-            log_name = argv[i+1];
-            log_limit = parse_ll_bounds(argv[i+3], 1, 1000000, &rc);
-            break;
         } else if (isdigit((unsigned char)argv[i][0])) {
             log_limit = parse_ll_bounds(argv[i], 1, 1000000, &rc);
             if (clear_log) {
@@ -297,13 +258,6 @@ shell_log_dump_cmd(int argc, char **argv)
             if (rc != 0) {
                 goto err;
             }
-        } else if (num_entries) {
-            rc = log_get_entries(log, log_limit, &entries);
-            if (!rc) {
-                console_printf("entries: %u\n", (unsigned int)entries);
-            } else {
-                console_printf("Invalid or empty log, rc=%d!\n", rc);
-            }
         } else {
             if (dump_logs) {
                 console_printf("Dumping log %s\n", log->l_name);
diff --git a/sys/log/full/syscfg.yml b/sys/log/full/syscfg.yml
index b245825c9..014e845ad 100644
--- a/sys/log/full/syscfg.yml
+++ b/sys/log/full/syscfg.yml
@@ -185,6 +185,11 @@ syscfg.defs:
             the global index to be sequentially increasing for persisted logs.
         value: 0
 
+    LOG_MAX_TRAILER_LEN:
+        description: >
+            Maximum length of trailer that can be appended to a log entry
+        value: "LF_MAX_ALIGN * 3"
+
 syscfg.vals.CONSOLE_TICKS:
     LOG_CONSOLE_PRETTY_WITH_TIMESTAMP: 0
 

Reply via email to