Add a generic implentation o seek function. Add test prototype.
[bertos.git] / kern / kfile.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 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Virtual KFile I/O interface.
34  * This module implement a standard fd.seek and a kfile
35  * test function.
36  *
37  * \version $Id$
38  * \author Francesco Sacchi <batt@develer.com>
39  * \author Daniele Basile <asterix@develer.com>
40  *
41  */
42
43 #include <kfile.h>
44
45 #inclede <appconfig.h>
46
47
48 /**
49  * Move \a fd file seek position of \a offset bytes
50  * from current position.
51  * This is a generic implementation of seek function, you should redefine
52  * it in your local module.
53  */
54 int32_t kfile_seek(struct _KFile *fd, kfile_off_t offset, KSeekMode whence)
55 {
56         uint32_t seek_pos;
57
58         switch(whence)
59         {
60
61         case KSM_SEEK_SET:
62                 seek_pos = 0;
63                 break;
64         case KSM_SEEK_END:
65                 seek_pos = fd->size - 1;
66                 break;
67         case KSM_SEEK_CUR:
68                 seek_pos = fd->seek_pos;
69                 break;
70         default:
71                 ASSERT(0);
72                 return -1;
73                 break;
74
75         }
76
77         /* Bound check */
78         if (seek_pos + offset > fd->size)
79         {
80                 ASSERT(0);
81                 return -1;
82         }
83
84         fd->seek_pos = seek_pos + offset;
85         kprintf("Flash seek to [%lu]\n", fd->seek_pos);
86
87         return fd->seek_pos;
88 }
89
90 #if CONFIG_TEST
91
92 /**
93  * Program memory read/write subtest.
94  * Try to write/read in the same \a f file location \a _size bytes.
95  * \return true if all is ok, false otherwise
96  * \note Restore file position at exit (if no error)
97  * \note Test buffer \a buf must be filled with
98  * the following statement:
99  * <pre>
100  * buf[i] = i & 0xff
101  * </pre>
102  */
103 static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t _size)
104 {
105         int32_t size = _size;
106
107         // Write test buffer
108         if (f->write(f, buf, size) != size)
109                 return false;
110         f->seek(f, -size, KSM_SEEK_CUR);
111
112         // Reset test buffer
113         memset(buf, 0, size);
114
115         // Read flash in test buffer
116         if (f->read(f, buf, size) != size)
117                 return false;
118         f->seek(f, -size, KSM_SEEK_CUR);
119
120         // Check test result
121         for (size_t i = 0; i < size; i++)
122                 if (buf[i] != (i & 0xff))
123                         return false;
124
125         return true;
126 }
127
128 /**
129  * Test for program memory read/write.
130  */
131 bool kfile_test(uint8_t *buf, size_t _size , uint8_t *save_buf, size_t * save_buf_size)
132 {
133         KFile fd;
134         int32_t size = _size;
135
136         /*
137          * Part of test buf size that you would write.
138          * This var is useded in test 3 to check fd.write
139          * when write outside size limit. Normaly we want
140          * perform a write until is space to write, otherwise
141          * we return.
142          */
143         int32_t len = size/2;
144
145         /*
146          * Fill in test buffer
147          */
148         for (int i = 0; i < size; i++)
149                 test_buf[i] = (i & 0xff);
150
151         kprintf("Generated test string..\n");
152
153         /*
154          * Open fd handler
155          */
156         fd.open(&fd, NULL, 0);
157         kprintf("Opened fd handler..\n");
158
159         /*
160          * If necessary, user could save content,
161          * for later restore.
162          */
163         if (save_buf != NULL)
164         {
165                 fd.read(&fd, save_buf, save_buf_size);
166                 kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
167         }
168
169         /* TEST 1 BEGIN. */
170         kprintf("Test 1: write from pos 0 to [%lu]\n", fd.size);
171
172         /*
173          * Seek to addr 0
174          */
175         if (fd.seek(&fd, 0, KSM_SEEK_SET) != 0)
176                 goto kfile_test_end;
177
178         kprintf("Seek to [%lu], expected[0]\n", fd.seek_pos);
179
180         /*
181          * Test flash read/write to address 0..size
182          */
183         if (!Kfile_rwTest(&fd, test_buf, size))
184                 goto kfile_test_end;
185
186         kprintf("Test 1: ok!\n");
187
188         /*
189          * Restore previous read content
190          */
191         if (save_buf != NULL)
192         {
193                 fd.seek(&fd, 0, KSM_SEEK_SET);
194
195                 if (fd.write(&fd, save_buf, save_buf_size) != size)
196                         goto kfile_test_end;
197
198                 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
199         }
200         /* TEST 1 END. */
201
202         /* TEST 2 BEGIN. */
203         kprintf("Test 2: write from pos [%lu] to [%lu]\n", fd.size/2 , size);
204
205         /*
206          * Go to half test size.
207          */
208         fd.seek(&fd, (fd.size/ 2), KSM_SEEK_SET);
209
210         kprintf("Seek to [%lu], expected[%lu]\n", fd.seek_pos, fd.size/2);
211
212         /*
213          * If necessary, user could save content,
214          * for later restore.
215          */
216         if (save_buf != NULL)
217         {
218                 fd.read(&fd, save_buf, save_buf_size);
219                 fd.seek(&fd, -size, KSM_SEEK_CUR);
220                 kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
221         }
222
223         /*
224          * Test flash read/write to address FLASHEND/2 ... FLASHEND/2 + size
225          */
226         if (!Kfile_rwTest(&fd, test_buf, size))
227                 goto kfile_test_end;
228
229         kprintf("Test 2: ok!\n");
230
231         /*
232          * Restore previous read content
233          */
234         if (save_buf != NULL)
235         {
236                 fd.seek(&fd, -size, KSM_SEEK_CUR);
237
238                 if (fd.write(&fd, save_buf, save_buf_size) != size)
239                         goto kfile_test_end;
240
241                 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
242         }
243
244         /* TEST 2 END. */
245
246         /* TEST 3 BEGIN. */
247         kprintf("Test 3: write outside of fd.size limit [%lu]\n", fd.size);
248
249         /*
250          * Go to the Flash end
251          */
252         fd.seek(&fd, -len, KSM_SEEK_END);
253         kprintf("Seek to [%lu], expected[%lu]\n", fd.seek_pos, fd.size - len);
254
255         /*
256          * If necessary, user could save content,
257          * for later restore.
258          */
259         if (save_buf != NULL)
260         {
261                 ASSERT(len > save_buf_size);
262
263                 fd.read(&fd, save_buf, len);
264                 fd.seek(&fd, -len, KSM_SEEK_CUR);
265                 kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + len);
266         }
267
268         /*
269          * Test flash read/write to address (FLASHEND - size) ... FLASHEND
270          */
271         if (!Kfile_rwTest(&fd, test_buf, size))
272                 goto kfile_test_end;
273
274         kprintf("Test 3: ok !\n");
275
276         /*
277          * Restore previous read content
278          */
279         if (save_buf != NULL)
280         {
281                 fd.seek(&fd, -len, KSM_SEEK_END);
282
283                 if (fd.write(&fd, save_buf, len) != len)
284                         goto kfile_test_end;
285
286                 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + len);
287         }
288
289         /* TEST 3 END. */
290
291         fd.close(&fd);
292         return true;
293
294 kfile_test_end:
295         fd.close(&fd);
296         return false;
297 }
298
299 #endif /* CONFIG_TEST */