Update preset.
[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  * \author Francesco Sacchi <batt@develer.com>
36  * \author Daniele Basile <asterix@develer.com>
37  *
38  */
39
40 #include "kfile_block.h"
41 #include <string.h>
42
43
44 /**
45  * ID for KFile over a KBlock.
46  */
47 #define KFT_KFILEBLOCK MAKE_ID('K', 'F', 'B', 'L')
48
49 /**
50  * Convert + ASSERT from generic KFile to KFileBlock.
51  */
52 INLINE KFileBlock * KFILEBLOCK_CAST(KFile *fd)
53 {
54         ASSERT(fd->_type == KFT_KFILEBLOCK);
55         return (KFileBlock *)fd;
56 }
57
58 #define KFILEBLOCK(dir, fd, buf, size) \
59 ({ \
60         KFileBlock *fb = KFILEBLOCK_CAST(fd); \
61         size_t len = 0; \
62         while (size) \
63         { \
64                 block_idx_t id = (fd)->seek_pos / fb->blk->blk_size; \
65                 if (id >= fb->blk->blk_cnt) \
66                         break; \
67                 size_t offset = (fd)->seek_pos % fb->blk->blk_size; \
68                 size_t count = MIN(size, (size_t)(fb->blk->blk_size - offset)); \
69                 size_t ret_len = kblock_##dir(fb->blk, id, buf, offset, count); \
70                 size -= ret_len; \
71                 (fd)->seek_pos += ret_len; \
72                 buf = buf + ret_len; \
73                 len += ret_len; \
74                 if (ret_len != count) \
75                         break; \
76         } \
77         len; \
78 })
79
80 static size_t kfileblock_read(struct KFile *fd, void *_buf, size_t size)
81 {
82         uint8_t *buf = (uint8_t *)_buf;
83         return KFILEBLOCK(read, fd, buf, size);
84 }
85
86 static size_t kfileblock_write(struct KFile *fd, const void *_buf, size_t size)
87 {
88         const uint8_t *buf = (const uint8_t *)_buf;
89         return KFILEBLOCK(write, fd, buf, size);
90 }
91
92 static int kfileblock_flush(struct KFile *fd)
93 {
94         KFileBlock *fb = KFILEBLOCK_CAST(fd);
95         return kblock_flush(fb->blk);
96 }
97
98 static int kfileblock_error(struct KFile *fd)
99 {
100         KFileBlock *fb = KFILEBLOCK_CAST(fd);
101         return kblock_error(fb->blk);
102 }
103
104 static void kfileblock_clearerr(struct KFile *fd)
105 {
106         KFileBlock *fb = KFILEBLOCK_CAST(fd);
107         return kblock_clearerr(fb->blk);
108 }
109
110 static int kfileblock_close(struct KFile *fd)
111 {
112         KFileBlock *fb = KFILEBLOCK_CAST(fd);
113         return kblock_close(fb->blk);
114 }
115
116 void kfileblock_init(KFileBlock *fb, KBlock *blk)
117 {
118         ASSERT(fb);
119         ASSERT(blk);
120         memset(fb, 0, sizeof(*fb));
121         kfile_init(&fb->fd);
122         DB(fb->fd._type = KFT_KFILEBLOCK);
123         fb->blk = blk;
124         fb->fd.size = blk->blk_cnt * blk->blk_size;
125         fb->fd.read = kfileblock_read;
126         fb->fd.write = kfileblock_write;
127         fb->fd.flush = kfileblock_flush;
128         fb->fd.error = kfileblock_error;
129         fb->fd.clearerr = kfileblock_clearerr;
130         fb->fd.close = kfileblock_close;
131 }