Split test suite in new file.
[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 implements some generic I/O interfaces for kfile.
35  *
36  * \version $Id$
37  * \author Francesco Sacchi <batt@develer.com>
38  * \author Daniele Basile <asterix@develer.com>
39  *
40  */
41
42
43 #include "kfile.h"
44 #include <appconfig.h>
45
46 #include <cfg/debug.h>
47 #include <mware/formatwr.h>
48 #include <string.h>
49
50 /*
51  * Sanity check for config parameters required by this module.
52  */
53 #if !defined(CONFIG_KFILE_GETS) || ((CONFIG_KFILE_GETS != 0) && CONFIG_KFILE_GETS != 1)
54         #error CONFIG_KFILE_GETS must be set to either 0 or 1 in appconfig.h
55 #endif
56 #if !defined(CONFIG_PRINTF)
57         #error CONFIG_PRINTF missing in appconfig.h
58 #endif
59
60
61 /**
62  * Generic putc() implementation using \a fd->write.
63  */
64 int kfile_putc(int _c, struct KFile *fd)
65 {
66         unsigned char c = (unsigned char)_c;
67
68         if (kfile_write(fd, &c, sizeof(c)) == sizeof(c))
69                 return (int)((unsigned char)_c);
70         else
71                 return EOF;
72 }
73
74 /**
75  * Generic getc() implementation using \a fd->read.
76  */
77 int kfile_getc(struct KFile *fd)
78 {
79         unsigned char c;
80
81         if (kfile_read(fd, &c, sizeof(c)) == sizeof(c))
82                 return (int)((unsigned char)c);
83         else
84                 return EOF;
85 }
86
87 #if CONFIG_PRINTF
88 /**
89  * Formatted write.
90  */
91 int kfile_printf(struct KFile *fd, const char *format, ...)
92 {
93         va_list ap;
94         int len;
95
96         va_start(ap, format);
97         len = _formatted_write(format, (void (*)(char, void *))kfile_putc, fd, ap);
98         va_end(ap);
99
100         return len;
101 }
102 #endif /* CONFIG_PRINTF */
103
104 /**
105  * Write a string to kfile \a fd.
106  * \return 0 if OK, EOF in case of error.
107  */
108 int kfile_print(struct KFile *fd, const char *s)
109 {
110         while (*s)
111         {
112                 if (kfile_putc(*s++, fd) == EOF)
113                         return EOF;
114         }
115         return 0;
116 }
117
118 #if CONFIG_KFILE_GETS
119 /**
120  * Read a line long at most as size and put it
121  * in buf.
122  * \return number of chars read or EOF in case
123  *         of error.
124  */
125 int kfile_gets(struct KFile *fd, char *buf, int size)
126 {
127         return kfile_gets_echo(fd, buf, size, false);
128 }
129
130
131 /**
132  * Read a line long at most as size and put it
133  * in buf, with optional echo.
134  *
135  * \return number of chars read, or EOF in case
136  *         of error.
137  */
138 int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo)
139 {
140         int i = 0;
141         int c;
142
143         for (;;)
144         {
145                 if ((c = kfile_getc(fd)) == EOF)
146                 {
147                         buf[i] = '\0';
148                         return -1;
149                 }
150
151                 /* FIXME */
152                 if (c == '\r' || c == '\n' || i >= size-1)
153                 {
154                         buf[i] = '\0';
155                         if (echo)
156                                 kfile_print(fd, "\r\n");
157                         break;
158                 }
159                 buf[i++] = c;
160                 if (echo)
161                         kfile_putc(c, fd);
162         }
163
164         return i;
165 }
166 #endif /* !CONFIG_KFILE_GETS */
167
168
169 /**
170  * Move \a fd file seek position of \a offset bytes from \a whence.
171  *
172  * This is a generic implementation of seek function, you can redefine
173  * it in your local module if needed.
174  */
175 kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence)
176 {
177         uint32_t seek_pos;
178
179         switch (whence)
180         {
181
182         case KSM_SEEK_SET:
183                 seek_pos = 0;
184                 break;
185         case KSM_SEEK_END:
186                 seek_pos = fd->size;
187                 break;
188         case KSM_SEEK_CUR:
189                 seek_pos = fd->seek_pos;
190                 break;
191         default:
192                 ASSERT(0);
193                 return EOF;
194                 break;
195         }
196
197         /* Bound check */
198         if (seek_pos + offset > fd->size)
199         {
200                 ASSERT(0);
201                 return EOF;
202         }
203
204         fd->seek_pos = seek_pos + offset;
205
206         return fd->seek_pos;
207 }
208
209 /**
210  * Reopen file \a fd.
211  * This is a generic implementation that only flush file
212  * and reset seek_pos to 0.
213  */
214 struct KFile * kfile_genericReopen(struct KFile *fd)
215 {
216         kfile_flush(fd);
217         kfile_seek(fd, 0, KSM_SEEK_SET);
218         return fd;
219 }
220
221 #if CONFIG_TEST_KFILE
222
223 /**
224  * KFile read/write subtest.
225  * Try to write/read in the same \a f file location \a size bytes.
226  * \return true if all is ok, false otherwise
227  * \note Restore file position at exit (if no error)
228  * \note Test buffer \a buf must be filled with
229  * the following statement:
230  * <pre>
231  * buf[i] = i & 0xff
232  * </pre>
233  */
234 static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t size)
235 {
236         /*
237          * Write test buffer
238          */
239         if (kfile_write(f, buf, size) != size)
240                 return false;
241
242         kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
243
244         /*
245          * Reset test buffer
246          */
247         memset(buf, 0, size);
248
249         /*
250          * Read file in test buffer
251          */
252         if (kfile_read(f, buf, size) != size)
253                 return false;
254         kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
255
256         /*
257          * Check test result
258          */
259         for (size_t i = 0; i < size; i++)
260                 if (buf[i] != (i & 0xff))
261                         return false;
262
263         return true;
264 }
265
266 /**
267  * KFile read/write test.
268  * This function write and read \a test_buf long \a size
269  * on \a fd handler.
270  * \a save_buf can be NULL or a buffer where to save previous file content.
271  */
272 bool kfile_test(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size)
273 {
274         /*
275          * Part of test buf size that you would write.
276          * This var is used in test 3 to check kfile_write
277          * when writing beyond filesize limit.
278          */
279         kfile_off_t len = size / 2;
280
281
282         /* Fill test buffer */
283         for (size_t i = 0; i < size; i++)
284                 test_buf[i] = (i & 0xff);
285
286         /*
287          * If necessary, user can save content,
288          * for later restore.
289          */
290         if (save_buf)
291         {
292                 kfile_read(fd, save_buf, size);
293                 kprintf("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
294         }
295
296         /* TEST 1 BEGIN. */
297         kprintf("Test 1: write from pos 0 to [%lu]\n", size);
298
299         /*
300          * Seek to addr 0.
301          */
302         if (kfile_seek(fd, 0, KSM_SEEK_SET) != 0)
303                 goto kfile_test_end;
304
305         /*
306          * Test read/write to address 0..size
307          */
308         if (!kfile_rwTest(fd, test_buf, size))
309                 goto kfile_test_end;
310
311         kprintf("Test 1: ok!\n");
312
313         /*
314          * Restore previous read content.
315          */
316         if (save_buf)
317         {
318                 kfile_seek(fd, 0, KSM_SEEK_SET);
319
320                 if (kfile_write(fd, save_buf, size) != size)
321                         goto kfile_test_end;
322
323                 kprintf("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
324         }
325         /* TEST 1 END. */
326
327         /* TEST 2 BEGIN. */
328         kprintf("Test 2: write from pos [%lu] to [%lu]\n", fd->size/2 , fd->size/2 + size);
329
330         /*
331          * Go to half test size.
332          */
333         kfile_seek(fd, (fd->size / 2), KSM_SEEK_SET);
334
335         /*
336          * If necessary, user can save content
337          * for later restore.
338          */
339         if (save_buf)
340         {
341                 kfile_read(fd, save_buf, size);
342                 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
343                 kprintf("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
344         }
345
346         /*
347          * Test read/write to address filesize/2 ... filesize/2 + size
348          */
349         if (!kfile_rwTest(fd, test_buf, size))
350                 goto kfile_test_end;
351
352         kprintf("Test 2: ok!\n");
353
354         /*
355          * Restore previous content.
356          */
357         if (save_buf)
358         {
359                 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
360
361                 if (kfile_write(fd, save_buf, size) != size)
362                         goto kfile_test_end;
363
364                 kprintf("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
365         }
366
367         /* TEST 2 END. */
368
369         /* TEST 3 BEGIN. */
370         kprintf("Test 3: write outside of fd->size limit [%lu]\n", fd->size);
371         kprintf("This test should FAIL!, you must see an assertion fail message.\n");
372
373         /*
374          * Go to the Flash end
375          */
376         kfile_seek(fd, -len, KSM_SEEK_END);
377
378         /*
379          * If necessary, user can save content,
380          * for later restore.
381          */
382         if (save_buf)
383         {
384                 kfile_read(fd, save_buf, len);
385                 kfile_seek(fd, -len, KSM_SEEK_CUR);
386                 kprintf("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + len);
387         }
388
389         /*
390          * Test read/write to address (filesize - size) ... filesize
391          */
392         if (kfile_rwTest(fd, test_buf, size))
393                 goto kfile_test_end;
394
395         kprintf("Test 3: ok!\n");
396
397         /*
398          * Restore previous read content
399          */
400         if (save_buf)
401         {
402                 kfile_seek(fd, -len, KSM_SEEK_END);
403
404                 if (kfile_write(fd, save_buf, len) != len)
405                         goto kfile_test_end;
406
407                 kprintf("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + len);
408         }
409
410         /* TEST 3 END. */
411
412         kfile_close(fd);
413         return true;
414
415 kfile_test_end:
416         kfile_close(fd);
417         return false;
418 }
419
420 #endif /* CONFIG_TEST */