Repository: incubator-mynewt-core Updated Branches: refs/heads/develop 745d6279d -> 3ad2ce23d
MYNEWT-335 - fcb_offset_last_n() changed to return a struct fcb_entry - Added a test case - Now properly returns error when there is no item at requested offset 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/e0d92a5d Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/e0d92a5d Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/e0d92a5d Branch: refs/heads/develop Commit: e0d92a5d34849310e0ddbb2179c3d960cf15cc91 Parents: d61c6cd Author: Fabio Utzig <ut...@utzig.org> Authored: Fri Feb 10 17:50:44 2017 -0800 Committer: Fabio Utzig <ut...@utzig.org> Committed: Mon Feb 13 10:52:51 2017 -0800 ---------------------------------------------------------------------- fs/fcb/include/fcb/fcb.h | 6 +- fs/fcb/src/fcb.c | 18 ++--- fs/fcb/test/src/fcb_test.c | 5 +- fs/fcb/test/src/testcases/fcb_test_last_of_n.c | 77 +++++++++++++++++++++ sys/log/full/src/log_fcb.c | 7 +- 5 files changed, 98 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e0d92a5d/fs/fcb/include/fcb/fcb.h ---------------------------------------------------------------------- diff --git a/fs/fcb/include/fcb/fcb.h b/fs/fcb/include/fcb/fcb.h index db19a2a..76f557a 100644 --- a/fs/fcb/include/fcb/fcb.h +++ b/fs/fcb/include/fcb/fcb.h @@ -127,8 +127,12 @@ int fcb_free_sector_cnt(struct fcb *fcb); */ int fcb_is_empty(struct fcb *fcb); +/* + * Element at offset *entries* from last position (backwards). + */ int -fcb_offset_last_n(struct fcb *fcb, uint8_t entries, uint32_t *last_n_off); +fcb_offset_last_n(struct fcb *fcb, uint8_t entries, + struct fcb_entry *last_n_entry); /* * Clears FCB passed to it http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e0d92a5d/fs/fcb/src/fcb.c ---------------------------------------------------------------------- diff --git a/fs/fcb/src/fcb.c b/fs/fcb/src/fcb.c index 0e210df..21bbc2f 100644 --- a/fs/fcb/src/fcb.c +++ b/fs/fcb/src/fcb.c @@ -205,14 +205,15 @@ fcb_sector_hdr_read(struct fcb *fcb, struct flash_area *fap, } /** - * Finds n-th element offset + * Finds the last n-th element fcb_entry (0 index means last element!) * @param0 ptr to fcb * @param1 n number of entries to calculate offset before - * @param2 ptr to the offset before to be returned + * @param2 ptr to the fcb_entry to be returned * @return 0 on success; non-zero on failure */ int -fcb_offset_last_n(struct fcb *fcb, uint8_t entries, uint32_t *last_n_off) +fcb_offset_last_n(struct fcb *fcb, uint8_t entries, + struct fcb_entry *last_n_entry) { struct fcb_entry loc; struct fcb_entry start; @@ -223,18 +224,17 @@ fcb_offset_last_n(struct fcb *fcb, uint8_t entries, uint32_t *last_n_off) while (!fcb_getnext(fcb, &loc)) { if (i == 0) { /* Start from the beginning of fcb entries */ - *last_n_off = loc.fe_elem_off; - start = loc; + *last_n_entry = start = loc; } - /* Update last_n_off after n entries and keep updating */ - if (i >= (entries - 1)) { + /* Update last_n_entry after n entries and keep updating */ + else if (i > entries) { fcb_getnext(fcb, &start); - *last_n_off = start.fe_elem_off; + *last_n_entry = start; } i++; } - return 0; + return (entries + 1) > i ? OS_ENOENT : 0; } /** http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e0d92a5d/fs/fcb/test/src/fcb_test.c ---------------------------------------------------------------------- diff --git a/fs/fcb/test/src/fcb_test.c b/fs/fcb/test/src/fcb_test.c index 77d2d3e..34a9be8 100644 --- a/fs/fcb/test/src/fcb_test.c +++ b/fs/fcb/test/src/fcb_test.c @@ -158,10 +158,10 @@ TEST_CASE_DECL(fcb_test_append_fill) TEST_CASE_DECL(fcb_test_reset) TEST_CASE_DECL(fcb_test_rotate) TEST_CASE_DECL(fcb_test_multiple_scratch) +TEST_CASE_DECL(fcb_test_last_of_n) TEST_SUITE(fcb_test_all) { - tu_case_set_pre_cb(fcb_tc_pretest, (void*)2); fcb_test_len(); @@ -188,6 +188,9 @@ TEST_SUITE(fcb_test_all) tu_case_set_pre_cb(fcb_tc_pretest, (void*)4); fcb_test_multiple_scratch(); + + tu_case_set_pre_cb(fcb_tc_pretest, (void*)4); + fcb_test_last_of_n(); } #if MYNEWT_VAL(SELFTEST) http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e0d92a5d/fs/fcb/test/src/testcases/fcb_test_last_of_n.c ---------------------------------------------------------------------- diff --git a/fs/fcb/test/src/testcases/fcb_test_last_of_n.c b/fs/fcb/test/src/testcases/fcb_test_last_of_n.c new file mode 100644 index 0000000..deebe54 --- /dev/null +++ b/fs/fcb/test/src/testcases/fcb_test_last_of_n.c @@ -0,0 +1,77 @@ +/* + * 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 "fcb_test.h" + +TEST_CASE(fcb_test_last_of_n) +{ + const uint8_t ENTRIES = 5; + struct fcb *fcb; + int rc; + struct fcb_entry loc; + struct fcb_entry areas[ENTRIES]; + uint8_t test_data[128]; + uint8_t i; + + fcb = &test_fcb; + fcb->f_scratch_cnt = 1; + + /* + * Add some fcbs. + */ + for (i = 0; i < ENTRIES; i++) { + rc = fcb_append(fcb, sizeof(test_data), &loc); + if (rc == FCB_ERR_NOSPACE) { + break; + } + + rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data, + sizeof(test_data)); + TEST_ASSERT(rc == 0); + + rc = fcb_append_finish(fcb, &loc); + TEST_ASSERT(rc == 0); + + areas[i] = loc; + } + + /* after last valid entry */ + rc = fcb_offset_last_n(fcb, 5, &loc); + assert (rc != 0); + + /* last entry */ + rc = fcb_offset_last_n(fcb, 0, &loc); + assert (rc == 0); + assert (areas[4].fe_area == loc.fe_area); + assert (areas[4].fe_data_off == loc.fe_data_off); + assert (areas[4].fe_data_len == loc.fe_data_len); + + /* somewhere in the middle */ + rc = fcb_offset_last_n(fcb, 2, &loc); + assert (rc == 0); + assert (areas[2].fe_area == loc.fe_area); + assert (areas[2].fe_data_off == loc.fe_data_off); + assert (areas[2].fe_data_len == loc.fe_data_len); + + /* first entry */ + rc = fcb_offset_last_n(fcb, 4, &loc); + assert (rc == 0); + assert (areas[0].fe_area == loc.fe_area); + assert (areas[0].fe_data_off == loc.fe_data_off); + assert (areas[0].fe_data_len == loc.fe_data_len); +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e0d92a5d/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 ea104d4..ed1f502 100644 --- a/sys/log/full/src/log_fcb.c +++ b/sys/log/full/src/log_fcb.c @@ -218,11 +218,10 @@ log_fcb_rtr_erase(struct log *log, void *arg) struct fcb fcb_scratch; struct fcb *fcb; const struct flash_area *ptr; - uint32_t offset; + struct fcb_entry entry; int rc; rc = 0; - offset = 0; if (!log) { rc = -1; goto err; @@ -249,13 +248,13 @@ log_fcb_rtr_erase(struct log *log, void *arg) } /* Calculate offset of n-th last entry */ - rc = fcb_offset_last_n(fcb, fcb_log->fl_entries, &offset); + rc = fcb_offset_last_n(fcb, fcb_log->fl_entries, &entry); if (rc) { goto err; } /* Copy to scratch */ - rc = log_fcb_copy(log, fcb, &fcb_scratch, offset); + rc = log_fcb_copy(log, fcb, &fcb_scratch, entry.fe_elem_off); if (rc) { goto err; }