Move unpack lwip ip address macro to macros module.
[bertos.git] / bertos / io / kblock_posix.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  * \author Francesco Sacchi <batt@develer.com>
34  *
35  * \brief KBlock interface over libc files.
36  *
37  * notest: avr
38  * notest: arm
39  */
40
41
42 #include "kblock_posix.h"
43 #include <string.h>
44 #include <stdio.h>
45
46
47 static int kblockposix_load(KBlock *b, block_idx_t index)
48 {
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;
52 }
53
54 static int kblockposix_store(struct KBlock *b, block_idx_t index)
55 {
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;
59 }
60
61 static size_t kblockposix_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
62 {
63         KBlockPosix *f = KBLOCKPOSIX_CAST(b);
64         memcpy(buf, (uint8_t *)f->b.priv.buf + offset, size);
65         return size;
66 }
67
68 static size_t kblockposix_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size)
69 {
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);
73 }
74
75 static size_t kblockposix_writeBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
76 {
77         KBlockPosix *f = KBLOCKPOSIX_CAST(b);
78         memcpy((uint8_t *)f->b.priv.buf + offset, buf, size);
79         return size;
80 }
81
82 static size_t kblockposix_writeDirect(struct KBlock *b, block_idx_t index, const void *buf, size_t offset, size_t size)
83 {
84         KBlockPosix *f = KBLOCKPOSIX_CAST(b);
85         ASSERT(buf);
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);
89 }
90
91 static int kblockposix_error(struct KBlock *b)
92 {
93         KBlockPosix *f = KBLOCKPOSIX_CAST(b);
94         return ferror(f->fp);
95 }
96
97
98 static void kblockposix_claererr(struct KBlock *b)
99 {
100         KBlockPosix *f = KBLOCKPOSIX_CAST(b);
101         clearerr(f->fp);
102 }
103
104
105 static int kblockposix_close(struct KBlock *b)
106 {
107         KBlockPosix *f = KBLOCKPOSIX_CAST(b);
108
109         return fflush(f->fp) | fclose(f->fp);
110 }
111
112
113 static const KBlockVTable kblockposix_hwbuffered_vt =
114 {
115         .readDirect = kblockposix_readDirect,
116
117         .readBuf = kblockposix_readBuf,
118         .writeBuf = kblockposix_writeBuf,
119         .load = kblockposix_load,
120         .store = kblockposix_store,
121
122         .error = kblockposix_error,
123         .clearerr = kblockposix_claererr,
124         .close = kblockposix_close,
125 };
126
127 static const KBlockVTable kblockposix_swbuffered_vt =
128 {
129         .readDirect = kblockposix_readDirect,
130         .writeDirect =kblockposix_writeDirect,
131
132         .readBuf = kblock_swReadBuf,
133         .writeBuf = kblock_swWriteBuf,
134         .load = kblock_swLoad,
135         .store = kblock_swStore,
136
137         .error = kblockposix_error,
138         .clearerr = kblockposix_claererr,
139         .close = kblockposix_close,
140 };
141
142 static const KBlockVTable kblockposix_unbuffered_vt =
143 {
144         .readDirect = kblockposix_readDirect,
145         .writeDirect =kblockposix_writeDirect,
146
147         .error = kblockposix_error,
148         .clearerr = kblockposix_claererr,
149         .close = kblockposix_close,
150 };
151
152
153
154 void kblockposix_init(KBlockPosix *f, FILE *fp, bool hwbuf, void *buf, size_t block_size, block_idx_t block_count)
155 {
156         ASSERT(f);
157         ASSERT(fp);
158         ASSERT(block_size);
159
160         memset(f, 0, sizeof(*f));
161
162         DB(f->b.priv.type = KBT_KBLOCKPOSIX);
163
164         f->fp = fp;
165         f->b.blk_size = block_size;
166         f->b.blk_cnt = block_count;
167
168         f->b.priv.flags |= KB_PARTIAL_WRITE;
169         if (buf)
170         {
171                 f->b.priv.flags |= KB_BUFFERED;
172                 f->b.priv.buf = buf;
173                 if (hwbuf)
174                         f->b.priv.vt = &kblockposix_hwbuffered_vt;
175                 else
176                         f->b.priv.vt = &kblockposix_swbuffered_vt;
177                 kblockposix_load(&f->b, 0);
178                 f->b.priv.curr_blk = 0;
179         }
180         else
181                 f->b.priv.vt = &kblockposix_unbuffered_vt;
182 }