From a7a659b9311a307ab1d32ab2b071537f6fcb6091 Mon Sep 17 00:00:00 2001 From: Vipul Rahane Date: Wed, 24 Jul 2024 12:23:50 -0700 Subject: [PATCH] sys/log: Reverse TLV implementation for number of entries support --- sys/log/full/selftest/util/src/log_test_util.c | 14 ++++++++------ .../testcases/log_test_case_cbmem_append_mbuf.c | 4 +++- .../log_test_case_cbmem_append_mbuf_body.c | 2 +- sys/log/full/src/log_cbmem.c | 13 ++++++------- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/sys/log/full/selftest/util/src/log_test_util.c b/sys/log/full/selftest/util/src/log_test_util.c index ad42f70144..6ce9aeaa51 100644 --- a/sys/log/full/selftest/util/src/log_test_util.c +++ b/sys/log/full/selftest/util/src/log_test_util.c @@ -49,8 +49,8 @@ static int ltu_str_max_idx = 0; struct dummy_log { struct log_entry_hdr hdr; - struct log_tlv tlv; uint32_t num_entries; + struct log_tlv tlv; }; struct dummy_log dummy_log = { @@ -71,11 +71,11 @@ struct dummy_log dummy_log = { .ue_level = 3, .ue_num_entries = 5 }, + .num_entries = 0, .tlv = { .tag = LOG_TLV_NUM_ENTRIES, .len = LOG_NUM_ENTRIES_SIZE }, - .num_entries = 0, }; char *ltu_str_logs[] = { @@ -126,11 +126,13 @@ ltu_init_arr(void) #endif memcpy(dummy_log_arr + offset, ltu_str_logs[i], strlen(ltu_str_logs[i])); offset += strlen(ltu_str_logs[i]); -#if MYNEWT_VAL(LOG_FLAGS_TLV_SUPPORT) && MYNEWT_VAL(LOG_TLV_NUM_ENTRIES) - memcpy(dummy_log_arr + offset, &dummy_log.tlv, sizeof(struct log_tlv)); - offset += sizeof(struct log_tlv); +#if MYNEWT_VAL(LOG_FLAGS_TLV_SUPPORT) +#if MYNEWT_VAL(LOG_TLV_NUM_ENTRIES) memcpy(dummy_log_arr + offset, &dummy_log.num_entries, LOG_NUM_ENTRIES_SIZE); offset += LOG_NUM_ENTRIES_SIZE; + memcpy(dummy_log_arr + offset, &dummy_log.tlv, sizeof(struct log_tlv)); + offset += sizeof(struct log_tlv); +#endif #endif } ltu_off_arr[i] = offset; @@ -300,7 +302,7 @@ ltu_walk_verify(struct log *log, struct log_offset *log_offset, TEST_ASSERT(rc == dlen); #if MYNEWT_VAL(LOG_FLAGS_TLV_SUPPORT) && MYNEWT_VAL(LOG_TLV_NUM_ENTRIES) - uint32_t num_entries;; + uint32_t num_entries; rc = log_read_trailer(log, dptr, LOG_TLV_NUM_ENTRIES, &num_entries); TEST_ASSERT(rc == 0); #endif diff --git a/sys/log/full/selftest/util/src/testcases/log_test_case_cbmem_append_mbuf.c b/sys/log/full/selftest/util/src/testcases/log_test_case_cbmem_append_mbuf.c index aaeab19d37..d94c4fe71d 100644 --- a/sys/log/full/selftest/util/src/testcases/log_test_case_cbmem_append_mbuf.c +++ b/sys/log/full/selftest/util/src/testcases/log_test_case_cbmem_append_mbuf.c @@ -28,6 +28,7 @@ TEST_CASE_SELF(log_test_case_cbmem_append_mbuf) uint16_t *off_arr; int i; int rc; + struct log_entry_hdr *hdr; int num_strs = ltu_num_strs(); ltu_setup_cbmem(&cbmem, &log); @@ -38,7 +39,8 @@ TEST_CASE_SELF(log_test_case_cbmem_append_mbuf) TEST_ASSERT_FATAL(off_arr != NULL); for (i = 0; i < num_strs; i++) { - len = off_arr[i+1] - off_arr[i]; + hdr = (struct log_entry_hdr *)(dummy_log_arr + off_arr[i]); + len = off_arr[i+1] - off_arr[i] - log_trailer_len(&log, hdr); /* Split chain into several mbufs. */ om = ltu_flat_to_fragged_mbuf(dummy_log_arr + off_arr[i], len, 2); diff --git a/sys/log/full/selftest/util/src/testcases/log_test_case_cbmem_append_mbuf_body.c b/sys/log/full/selftest/util/src/testcases/log_test_case_cbmem_append_mbuf_body.c index 86775f8077..4dd0b026c1 100644 --- a/sys/log/full/selftest/util/src/testcases/log_test_case_cbmem_append_mbuf_body.c +++ b/sys/log/full/selftest/util/src/testcases/log_test_case_cbmem_append_mbuf_body.c @@ -41,7 +41,7 @@ TEST_CASE_SELF(log_test_case_cbmem_append_mbuf_body) for (i = 0; i < num_strs; i++) { hdr = (struct log_entry_hdr *)(dummy_log_arr + off_arr[i]); len = off_arr[i + 1] - off_arr[i] - - log_hdr_len(hdr); + log_hdr_len(hdr) - log_trailer_len(&log, hdr); /* Split chain into several mbufs. */ om = ltu_flat_to_fragged_mbuf(dummy_log_arr + off_arr[i] + log_hdr_len(hdr), diff --git a/sys/log/full/src/log_cbmem.c b/sys/log/full/src/log_cbmem.c index 5b4e73cafa..dca84eec49 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; - uint16_t num_tlvs = 0; + uint8_t num_tlvs = 0; struct cbmem_scat_gath sg = { .entries = (struct cbmem_scat_gath_entry[]) { { @@ -79,7 +79,7 @@ log_cbmem_append_body(struct log *log, const struct log_entry_hdr *hdr, #endif #if MYNEWT_VAL(LOG_TLV_NUM_TLVS) /* Number of TLVs is only written if there are more than one TLVs */ - if (num_tlvs > 0) { + if (num_tlvs > 1) { sg.entries[6].flat_buf = &(struct log_tlv) {LOG_NUM_TLVS_SIZE, LOG_TLV_NUM_TLVS}; sg.entries[6].flat_len = sizeof(struct log_tlv); sg.entries[5].flat_len = LOG_NUM_TLVS_SIZE; @@ -115,7 +115,7 @@ log_cbmem_append_mbuf_body(struct log *log, const struct log_entry_hdr *hdr, { int rc = 0; struct cbmem *cbmem; - uint16_t num_tlvs = 0; + uint8_t num_tlvs = 0; struct cbmem_scat_gath sg = { .entries = (struct cbmem_scat_gath_entry[]) { { @@ -166,7 +166,7 @@ log_cbmem_append_mbuf_body(struct log *log, const struct log_entry_hdr *hdr, #endif #if MYNEWT_VAL(LOG_TLV_NUM_TLVS) /* Number of TLVs is only written if there are more than one TLVs */ - if (num_tlvs > 0) { + if (num_tlvs > 1) { sg.entries[6].flat_buf = &(struct log_tlv) {LOG_NUM_TLVS_SIZE, LOG_TLV_NUM_TLVS}; sg.entries[6].flat_len = sizeof(struct log_tlv); sg.entries[5].flat_len = LOG_NUM_TLVS_SIZE; @@ -204,7 +204,7 @@ log_cbmem_append_mbuf(struct log *log, struct os_mbuf *om) * time is so that we account for the image hash as well. */ - os_mbuf_pullup(om, LOG_BASE_ENTRY_HDR_SIZE); + om = os_mbuf_pullup(om, LOG_BASE_ENTRY_HDR_SIZE); /* * We can just pass the om->om_data ptr as the log_entry_hdr @@ -213,10 +213,9 @@ log_cbmem_append_mbuf(struct log *log, struct os_mbuf *om) */ hdr_len = log_hdr_len((struct log_entry_hdr *)om->om_data); - os_mbuf_pullup(om, hdr_len); + om = os_mbuf_pullup(om, hdr_len); memcpy(&hdr, om->om_data, hdr_len); - os_mbuf_adj(om, hdr_len); rc = log_cbmem_append_mbuf_body(log, &hdr, om);