Reformat;Increase NAC timeout; Fix warning; Add simple write test.
[bertos.git] / bertos / io / kblock.c
index c36fd0db454bb0319724cce85ff5bdee301dc902..def111cb598bcfcbdfe1c6a2e1f7bc060e31c393 100644 (file)
 #define LOG_FORMAT LOG_FMT_VERBOSE
 
 #include <cfg/log.h>
+#include <string.h>
 
 INLINE size_t kblock_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size)
 {
        KB_ASSERT_METHOD(b, readDirect);
+       ASSERT(index < b->blk_cnt);
        return b->priv.vt->readDirect(b, b->priv.blk_start + index, buf, offset, size);
 }
 
+INLINE int kblock_writeBlock(struct KBlock *b, block_idx_t index, const void *buf)
+{
+       KB_ASSERT_METHOD(b, writeBlock);
+       ASSERT(index < b->blk_cnt);
+       return b->priv.vt->writeBlock(b, b->priv.blk_start + index, buf);
+}
+
 INLINE size_t kblock_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
 {
        KB_ASSERT_METHOD(b, readBuf);
@@ -82,38 +91,27 @@ INLINE int kblock_store(struct KBlock *b, block_idx_t index)
        return b->priv.vt->store(b, b->priv.blk_start + index);
 }
 
+INLINE void kblock_setDirty(struct KBlock *b, bool dirty)
+{
+       if (dirty)
+               b->priv.flags |= KB_CACHE_DIRTY;
+       else
+               b->priv.flags &= ~KB_CACHE_DIRTY;
+}
 
 
-size_t kblock_read(struct KBlock *b, block_idx_t idx, void *_buf, size_t offset, size_t size)
-{
-       size_t tot_rd = 0;
-       uint8_t *buf = (uint8_t *)_buf;
 
+size_t kblock_read(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size)
+{
        ASSERT(b);
        ASSERT(buf);
+       ASSERT(offset + size <= b->blk_size);
        LOG_INFO("blk_idx %d, offset %d, size %d\n", idx, offset, size);
 
-       while (size)
-       {
-               size_t len = MIN(size, b->blk_size - offset);
-               size_t rlen;
-
-               if (idx == b->priv.curr_blk)
-                       rlen = kblock_readBuf(b, buf, offset, len);
-               else
-                       rlen = kblock_readDirect(b, idx, buf, offset, len);
-
-               tot_rd += rlen;
-               if (rlen != len)
-                       break;
-
-               idx++;
-               size -= rlen;
-               offset = 0;
-               buf += rlen;
-       }
-
-       return tot_rd;
+       if (kblock_buffered(b) && idx == b->priv.curr_blk)
+               return kblock_readBuf(b, buf, offset, size);
+       else
+               return kblock_readDirect(b, idx, buf, offset, size);
 }
 
 
@@ -121,11 +119,11 @@ int kblock_flush(struct KBlock *b)
 {
        ASSERT(b);
 
-       if (b->priv.cache_dirty)
+       if (kblock_cacheDirty(b))
        {
                LOG_INFO("flushing block %d\n", b->priv.curr_blk);
                if (kblock_store(b, b->priv.curr_blk) == 0)
-                       b->priv.cache_dirty = false;
+                       kblock_setDirty(b, false);
                else
                        return EOF;
        }
@@ -149,48 +147,69 @@ static bool kblock_loadPage(struct KBlock *b, block_idx_t idx)
 }
 
 
-size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *_buf, size_t offset, size_t size)
+size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size)
 {
-       size_t tot_wr = 0;
-       const uint8_t *buf = (const uint8_t *)_buf;
-
        ASSERT(b);
        ASSERT(buf);
+       ASSERT(idx < b->blk_cnt);
+       ASSERT(offset + size <= b->blk_size);
 
        LOG_INFO("blk_idx %d, offset %d, size %d\n", idx, offset, size);
-       while (size)
+       
+       if (kblock_buffered(b))
        {
-               size_t len = MIN(size, b->blk_size - offset);
-               size_t wlen;
-
                if (!kblock_loadPage(b, idx))
-                       break;
+                       return 0;
 
-               wlen = kblock_writeBuf(b, buf, offset, len);
-               b->priv.cache_dirty = true;
-
-               tot_wr += wlen;
-               if (wlen != len)
-                       break;
-
-               idx++;
-               size -= wlen;
-               offset = 0;
-               buf += wlen;
+               kblock_setDirty(b, true);
+               return kblock_writeBuf(b, buf, offset, size);
+       }
+       else
+       {
+               ASSERT(offset == 0);
+               ASSERT(size == b->blk_size);
+               return (kblock_writeBlock(b, idx, buf) == 0) ? size : 0;
        }
-
-       return tot_wr;
 }
 
 int kblock_copy(struct KBlock *b, block_idx_t idx1, block_idx_t idx2)
 {
        ASSERT(b);
+       ASSERT(idx1 < b->blk_cnt);
+       ASSERT(idx2 < b->blk_cnt);
+       ASSERT(kblock_buffered(b));
 
        if (!kblock_loadPage(b, idx1))
                return EOF;
 
        b->priv.curr_blk = idx2;
-       b->priv.cache_dirty = true;
+       kblock_setDirty(b, true);
        return 0;
 }
 
+int kblock_swLoad(struct KBlock *b, block_idx_t index)
+{
+       return (kblock_readDirect(b, index, b->priv.buf, 0, b->blk_size) == b->blk_size) ? 0 : EOF;
+}
+
+int kblock_swStore(struct KBlock *b, block_idx_t index)
+{
+       return kblock_writeBlock(b, index, b->priv.buf);
+}
+
+size_t kblock_swReadBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
+{
+       ASSERT(buf);
+       ASSERT(offset + size <= b->blk_size);
+       
+       memcpy(buf, (uint8_t *)b->priv.buf + offset, size);
+       return size;
+}
+
+size_t kblock_swWriteBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
+{
+       ASSERT(buf);
+       ASSERT(offset + size <= b->blk_size);
+       memcpy((uint8_t *)b->priv.buf + offset, buf, size);
+       return size;
+}