From: batt Date: Mon, 2 Aug 2010 12:55:16 +0000 (+0000) Subject: Add KFile over a KBlock device (first implementation). X-Git-Tag: 2.6.0~271 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=0142058531b1f00b0dbe740b43900dfc361656e5;p=bertos.git Add KFile over a KBlock device (first implementation). git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4119 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/io/kfile_block.c b/bertos/io/kfile_block.c new file mode 100644 index 00000000..569e1c76 --- /dev/null +++ b/bertos/io/kfile_block.c @@ -0,0 +1,115 @@ +/** + * \file + * + * + * \brief KFile interface over a KBlock. + * + */ + +#include "kfile_block.h" +#include + + +/** + * ID for KFile over a KBlock. + */ +#define KFT_KFILEBLOCK MAKE_ID('K', 'F', 'B', 'L') + +/** + * Convert + ASSERT from generic KFile to KFileBlock. + */ +INLINE KFileBlock * KFILEBLOCK_CAST(KFile *fd) +{ + ASSERT(fd->_type == KFT_KFILEBLOCK); + return (KFileBlock *)fd; +} + +#define KFILEBLOCK(dir, fd, buf, size) \ +({ \ + KFileBlock *fb = KFILEBLOCK_CAST(fd); \ + block_idx_t id = (fd)->seek_pos / fb->b->blk_size; \ + size_t offset = (fd)->seek_pos % fb->b->blk_size; \ + size_t len = kblock_##dir(fb->b, id, buf, offset, size); \ + (fd)->seek_pos += len; \ + len; \ +}) + +static size_t kfileblock_read(struct KFile *fd, void *buf, size_t size) +{ + return KFILEBLOCK(read, fd, buf, size); +} + +static size_t kfileblock_write(struct KFile *fd, const void *buf, size_t size) +{ + return KFILEBLOCK(write, fd, buf, size); +} + +static int kfileblock_flush(struct KFile *fd) +{ + KFileBlock *fb = KFILEBLOCK_CAST(fd); + return kblock_flush(fb->b); +} + +static int kfileblock_error(struct KFile *fd) +{ + KFileBlock *fb = KFILEBLOCK_CAST(fd); + return kblock_error(fb->b); +} + +static void kfileblock_clearerr(struct KFile *fd) +{ + KFileBlock *fb = KFILEBLOCK_CAST(fd); + return kblock_clearerr(fb->b); +} + +static int kfileblock_close(struct KFile *fd) +{ + KFileBlock *fb = KFILEBLOCK_CAST(fd); + return kblock_close(fb->b); +} + +void kfileblock_init(KFileBlock *fb, KBlock *b) +{ + ASSERT(fb); + ASSERT(b); + ASSERT(kblock_partialWrite(b)); + memset(fb, 0, sizeof(*fb)); + kfile_init(&fb->fd); + DB(fb->fd._type = KFT_KFILEBLOCK); + fb->b = b; + fb->fd.size = b->blk_cnt * b->blk_size; + fb->fd.read = kfileblock_read; + fb->fd.write = kfileblock_write; + fb->fd.flush = kfileblock_flush; + fb->fd.error = kfileblock_error; + fb->fd.clearerr = kfileblock_clearerr; + fb->fd.close = kfileblock_close; +} diff --git a/bertos/io/kfile_block.h b/bertos/io/kfile_block.h new file mode 100644 index 00000000..9694ea94 --- /dev/null +++ b/bertos/io/kfile_block.h @@ -0,0 +1,52 @@ +/** + * \file + * + * + * \brief KFile interface over a KBlock. + * + */ + +#ifndef IO_KFILE_BLOCK_H +#define IO_KFILE_BLOCK_H + +#include +#include +#include + +typedef struct KFileBlock +{ + KFile fd; + KBlock *b; +} KFileBlock; + +void kfileblock_init(KFileBlock *fb, KBlock *b); + +#endif /* IO_KFILE_KBLOCK_H */