mynewt-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vipulrah...@apache.org
Subject [25/50] incubator-mynewt-core git commit: Fix review issues of fcb_offset_last_n
Date Thu, 23 Feb 2017 22:33:19 GMT
Fix review issues of fcb_offset_last_n


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

Branch: refs/heads/sensors_branch
Commit: 925b6bb7e964bf6fbce244596731bf68c975771d
Parents: e0d92a5
Author: Fabio Utzig <utzig@utzig.org>
Authored: Wed Feb 15 15:07:18 2017 -0800
Committer: Fabio Utzig <utzig@utzig.org>
Committed: Wed Feb 15 15:07:18 2017 -0800

----------------------------------------------------------------------
 fs/fcb/src/fcb.c                               | 21 ++++++++++++---------
 fs/fcb/test/src/testcases/fcb_test_last_of_n.c | 21 ++++++++++++++-------
 2 files changed, 26 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/925b6bb7/fs/fcb/src/fcb.c
----------------------------------------------------------------------
diff --git a/fs/fcb/src/fcb.c b/fs/fcb/src/fcb.c
index 21bbc2f..0a68be0 100644
--- a/fs/fcb/src/fcb.c
+++ b/fs/fcb/src/fcb.c
@@ -205,36 +205,39 @@ fcb_sector_hdr_read(struct fcb *fcb, struct flash_area *fap,
 }
 
 /**
- * Finds the last n-th element fcb_entry (0 index means last element!)
+ * Finds the fcb entry that gives back upto n entries at the end.
  * @param0 ptr to fcb
- * @param1 n number of entries to calculate offset before
+ * @param1 n number of fcb entries the user wants to get
  * @param2 ptr to the fcb_entry to be returned
- * @return 0 on success; non-zero on failure
+ * @return 0 on there are any fcbs aviable; OS_ENOENT otherwise
  */
 int
 fcb_offset_last_n(struct fcb *fcb, uint8_t entries,
         struct fcb_entry *last_n_entry)
 {
     struct fcb_entry loc;
-    struct fcb_entry start;
     int i;
 
+    /* assure a minimum amount of entries */
+    if (!entries) {
+        entries = 1;
+    }
+
     i = 0;
     memset(&loc, 0, sizeof(loc));
     while (!fcb_getnext(fcb, &loc)) {
         if (i == 0) {
             /* Start from the beginning of fcb entries */
-            *last_n_entry = start = loc;
+            *last_n_entry = loc;
         }
         /* Update last_n_entry after n entries and keep updating */
-        else if (i > entries) {
-            fcb_getnext(fcb, &start);
-            *last_n_entry = start;
+        else if (i > (entries - 1)) {
+            fcb_getnext(fcb, last_n_entry);
         }
         i++;
     }
 
-    return (entries + 1) > i ? OS_ENOENT : 0;
+    return (i == 0) ? OS_ENOENT : 0;
 }
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/925b6bb7/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
index deebe54..5ada743 100644
--- a/fs/fcb/test/src/testcases/fcb_test_last_of_n.c
+++ b/fs/fcb/test/src/testcases/fcb_test_last_of_n.c
@@ -31,6 +31,10 @@ TEST_CASE(fcb_test_last_of_n)
     fcb = &test_fcb;
     fcb->f_scratch_cnt = 1;
 
+    /* No fcbs available */
+    rc = fcb_offset_last_n(fcb, 1, &loc);
+    assert (rc != 0);
+
     /*
      * Add some fcbs.
      */
@@ -50,26 +54,29 @@ TEST_CASE(fcb_test_last_of_n)
         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);
+    rc = fcb_offset_last_n(fcb, 1, &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);
+    rc = fcb_offset_last_n(fcb, 3, &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);
+    rc = fcb_offset_last_n(fcb, 5, &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);
+
+    /* after last valid entry, returns the first one like for 5 */
+    rc = fcb_offset_last_n(fcb, 6, &loc);
     assert (rc == 0);
     assert (areas[0].fe_area == loc.fe_area);
     assert (areas[0].fe_data_off == loc.fe_data_off);


Mime
View raw message