4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \author Francesco Sacchi <batt@develer.com>
35 * \brief KBlock interface over libc files.
42 #include "kblock_posix.h"
47 static int kblockposix_load(KBlock *b, block_idx_t index)
49 KBlockPosix *f = KBLOCKPOSIX_CAST(b);
50 fseek(f->fp, index * b->blk_size, SEEK_SET);
51 return (fread(f->b.priv.buf, 1, b->blk_size, f->fp) == b->blk_size) ? 0 : EOF;
54 static int kblockposix_store(struct KBlock *b, block_idx_t index)
56 KBlockPosix *f = KBLOCKPOSIX_CAST(b);
57 fseek(f->fp, index * b->blk_size, SEEK_SET);
58 return (fwrite(f->b.priv.buf, 1, b->blk_size, f->fp) == b->blk_size) ? 0 : EOF;
61 static size_t kblockposix_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
63 KBlockPosix *f = KBLOCKPOSIX_CAST(b);
64 memcpy(buf, (uint8_t *)f->b.priv.buf + offset, size);
68 static size_t kblockposix_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size)
70 KBlockPosix *f = KBLOCKPOSIX_CAST(b);
71 fseek(f->fp, index * b->blk_size + offset, SEEK_SET);
72 return fread(buf, 1, size, f->fp);
75 static size_t kblockposix_writeBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
77 KBlockPosix *f = KBLOCKPOSIX_CAST(b);
78 memcpy((uint8_t *)f->b.priv.buf + offset, buf, size);
82 static size_t kblockposix_writeDirect(struct KBlock *b, block_idx_t index, const void *buf, size_t offset, size_t size)
84 KBlockPosix *f = KBLOCKPOSIX_CAST(b);
86 ASSERT(index < b->blk_cnt);
87 fseek(f->fp, index * b->blk_size + offset, SEEK_SET);
88 return fwrite(buf, 1, size, f->fp);
91 static int kblockposix_error(struct KBlock *b)
93 KBlockPosix *f = KBLOCKPOSIX_CAST(b);
98 static void kblockposix_claererr(struct KBlock *b)
100 KBlockPosix *f = KBLOCKPOSIX_CAST(b);
105 static int kblockposix_close(struct KBlock *b)
107 KBlockPosix *f = KBLOCKPOSIX_CAST(b);
109 return fflush(f->fp) | fclose(f->fp);
113 static const KBlockVTable kblockposix_hwbuffered_vt =
115 .readDirect = kblockposix_readDirect,
117 .readBuf = kblockposix_readBuf,
118 .writeBuf = kblockposix_writeBuf,
119 .load = kblockposix_load,
120 .store = kblockposix_store,
122 .error = kblockposix_error,
123 .clearerr = kblockposix_claererr,
124 .close = kblockposix_close,
127 static const KBlockVTable kblockposix_swbuffered_vt =
129 .readDirect = kblockposix_readDirect,
130 .writeDirect =kblockposix_writeDirect,
132 .readBuf = kblock_swReadBuf,
133 .writeBuf = kblock_swWriteBuf,
134 .load = kblock_swLoad,
135 .store = kblock_swStore,
137 .error = kblockposix_error,
138 .clearerr = kblockposix_claererr,
139 .close = kblockposix_close,
142 static const KBlockVTable kblockposix_unbuffered_vt =
144 .readDirect = kblockposix_readDirect,
145 .writeDirect =kblockposix_writeDirect,
147 .error = kblockposix_error,
148 .clearerr = kblockposix_claererr,
149 .close = kblockposix_close,
154 void kblockposix_init(KBlockPosix *f, FILE *fp, bool hwbuf, void *buf, size_t block_size, block_idx_t block_count)
160 memset(f, 0, sizeof(*f));
162 DB(f->b.priv.type = KBT_KBLOCKPOSIX);
165 f->b.blk_size = block_size;
166 f->b.blk_cnt = block_count;
168 f->b.priv.flags |= KB_PARTIAL_WRITE;
171 f->b.priv.flags |= KB_BUFFERED;
174 f->b.priv.vt = &kblockposix_hwbuffered_vt;
176 f->b.priv.vt = &kblockposix_swbuffered_vt;
177 kblockposix_load(&f->b, 0);
178 f->b.priv.curr_blk = 0;
181 f->b.priv.vt = &kblockposix_unbuffered_vt;