Add KFile over a KBlock device (first implementation).
[bertos.git] / bertos / io / kfile_block.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief KFile interface over a KBlock.
34  *
35  */
36
37 #include "kfile_block.h"
38 #include <string.h>
39
40
41 /**
42  * ID for KFile over a KBlock.
43  */
44 #define KFT_KFILEBLOCK MAKE_ID('K', 'F', 'B', 'L')
45
46 /**
47  * Convert + ASSERT from generic KFile to KFileBlock.
48  */
49 INLINE KFileBlock * KFILEBLOCK_CAST(KFile *fd)
50 {
51         ASSERT(fd->_type == KFT_KFILEBLOCK);
52         return (KFileBlock *)fd;
53 }
54
55 #define KFILEBLOCK(dir, fd, buf, size) \
56 ({ \
57         KFileBlock *fb = KFILEBLOCK_CAST(fd); \
58         block_idx_t id = (fd)->seek_pos / fb->b->blk_size; \
59         size_t offset = (fd)->seek_pos % fb->b->blk_size; \
60         size_t len = kblock_##dir(fb->b, id, buf, offset, size); \
61         (fd)->seek_pos += len; \
62         len; \
63 })
64
65 static size_t kfileblock_read(struct KFile *fd, void *buf, size_t size)
66 {
67         return KFILEBLOCK(read, fd, buf, size);
68 }
69
70 static size_t kfileblock_write(struct KFile *fd, const void *buf, size_t size)
71 {
72         return KFILEBLOCK(write, fd, buf, size);
73 }
74
75 static int kfileblock_flush(struct KFile *fd)
76 {
77         KFileBlock *fb = KFILEBLOCK_CAST(fd);
78         return kblock_flush(fb->b);
79 }
80
81 static int kfileblock_error(struct KFile *fd)
82 {
83         KFileBlock *fb = KFILEBLOCK_CAST(fd);
84         return kblock_error(fb->b);
85 }
86
87 static void kfileblock_clearerr(struct KFile *fd)
88 {
89         KFileBlock *fb = KFILEBLOCK_CAST(fd);
90         return kblock_clearerr(fb->b);
91 }
92
93 static int kfileblock_close(struct KFile *fd)
94 {
95         KFileBlock *fb = KFILEBLOCK_CAST(fd);
96         return kblock_close(fb->b);
97 }
98
99 void kfileblock_init(KFileBlock *fb, KBlock *b)
100 {
101         ASSERT(fb);
102         ASSERT(b);
103         ASSERT(kblock_partialWrite(b));
104         memset(fb, 0, sizeof(*fb));
105         kfile_init(&fb->fd);
106         DB(fb->fd._type = KFT_KFILEBLOCK);
107         fb->b = b;
108         fb->fd.size = b->blk_cnt * b->blk_size;
109         fb->fd.read = kfileblock_read;
110         fb->fd.write = kfileblock_write;
111         fb->fd.flush = kfileblock_flush;
112         fb->fd.error = kfileblock_error;
113         fb->fd.clearerr = kfileblock_clearerr;
114         fb->fd.close = kfileblock_close;
115 }