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 2007 Develer S.r.l. (http://www.develer.com/)
33 * \brief Virtual KFile I/O interface.
34 * This module implements some generic I/O interfaces for kfile.
37 * \author Francesco Sacchi <batt@develer.com>
38 * \author Daniele Basile <asterix@develer.com>
44 #include <appconfig.h>
46 #include <cfg/debug.h>
47 #include <mware/formatwr.h>
51 * Sanity check for config parameters required by this module.
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
56 #if !defined(CONFIG_PRINTF)
57 #error CONFIG_PRINTF missing in appconfig.h
62 * Generic putc implementation using \a fd->write.
64 int kfile_putc(int _c, struct KFile *fd)
66 unsigned char c = (unsigned char)_c;
68 if (kfile_write(fd, &c, sizeof(c)) == sizeof(c))
69 return (int)((unsigned char)_c);
75 * Generic getc implementation using \a fd->read.
77 int kfile_getc(struct KFile *fd)
81 if (kfile_read(fd, &c, sizeof(c)) == sizeof(c))
82 return (int)((unsigned char)c);
91 int kfile_printf(struct KFile *fd, const char *format, ...)
97 len = _formatted_write(format, (void (*)(char, void *))kfile_putc, fd, ap);
102 #endif /* CONFIG_PRINTF */
105 * Write a string to kfile \a fd.
106 * \return 0 if OK, EOF in case of error.
108 int kfile_print(struct KFile *fd, const char *s)
112 if (kfile_putc(*s++, fd) == EOF)
118 #if CONFIG_KFILE_GETS
120 * Read a line long at most as size and put it
122 * \return number of chars read or EOF in case
125 int kfile_gets(struct KFile *fd, char *buf, int size)
127 return kfile_gets_echo(fd, buf, size, false);
132 * Read a line long at most as size and put it
133 * in buf, with optional echo.
135 * \return number of chars read, or EOF in case
138 int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo)
145 if ((c = kfile_getc(fd)) == EOF)
152 if (c == '\r' || c == '\n' || i >= size-1)
156 kfile_print(fd, "\r\n");
166 #endif /* !CONFIG_KFILE_GETS */
170 * Move \a fd file seek position of \a offset bytes from \a whence.
172 * This is a generic implementation of seek function, you can redefine
173 * it in your local module is needed.
175 kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence)
186 seek_pos = fd->size - 1;
189 seek_pos = fd->seek_pos;
198 if (seek_pos + offset > fd->size)
204 fd->seek_pos = seek_pos + offset;
212 * KFile read/write subtest.
213 * Try to write/read in the same \a f file location \a size bytes.
214 * \return true if all is ok, false otherwise
215 * \note Restore file position at exit (if no error)
216 * \note Test buffer \a buf must be filled with
217 * the following statement:
222 static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t size)
227 if (kfile_write(f, buf, size) != size)
230 kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
235 memset(buf, 0, size);
238 * Read file in test buffer
240 if (kfile_read(f, buf, size) != size)
242 kfile_seek(f, -(kfile_off_t)size, KSM_SEEK_CUR);
247 for (size_t i = 0; i < size; i++)
248 if (buf[i] != (i & 0xff))
255 * KFile read/write test.
256 * This function write and read \a test_buf long \a size
258 * \a save_buf can be NULL or a buffer where to save previous file content.
260 bool kfile_test(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size)
263 * Part of test buf size that you would write.
264 * This var is used in test 3 to check kfile_write
265 * when writing beyond filesize limit.
267 kfile_off_t len = size / 2;
270 /* Fill test buffer */
271 for (size_t i = 0; i < size; i++)
272 test_buf[i] = (i & 0xff);
275 * If necessary, user can save content,
280 kfile_read(fd, save_buf, size);
281 kprintf("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
285 kprintf("Test 1: write from pos 0 to [%lu]\n", size);
290 if (kfile_seek(fd, 0, KSM_SEEK_SET) != 0)
294 * Test read/write to address 0..size
296 if (!kfile_rwTest(fd, test_buf, size))
299 kprintf("Test 1: ok!\n");
302 * Restore previous read content.
306 kfile_seek(fd, 0, KSM_SEEK_SET);
308 if (kfile_write(fd, save_buf, size) != size)
311 kprintf("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
316 kprintf("Test 2: write from pos [%lu] to [%lu]\n", fd->size/2 , fd->size/2 + size);
319 * Go to half test size.
321 kfile_seek(fd, (fd->size / 2), KSM_SEEK_SET);
324 * If necessary, user can save content
329 kfile_read(fd, save_buf, size);
330 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
331 kprintf("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
335 * Test read/write to address filesize/2 ... filesize/2 + size
337 if (!kfile_rwTest(fd, test_buf, size))
340 kprintf("Test 2: ok!\n");
343 * Restore previous content.
347 kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR);
349 if (kfile_write(fd, save_buf, size) != size)
352 kprintf("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size);
358 kprintf("Test 3: write outside of fd->size limit [%lu]\n", fd->size);
359 kprintf("This test should FAIL!, you must see an assertion fail message.\n");
362 * Go to the Flash end
364 kfile_seek(fd, -len, KSM_SEEK_END);
367 * If necessary, user can save content,
372 kfile_read(fd, save_buf, len);
373 kfile_seek(fd, -len, KSM_SEEK_CUR);
374 kprintf("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + len);
378 * Test read/write to address (filesize - size) ... filesize
380 if (kfile_rwTest(fd, test_buf, size))
383 kprintf("Test 3: ok!\n");
386 * Restore previous read content
390 kfile_seek(fd, -len, KSM_SEEK_END);
392 if (kfile_write(fd, save_buf, len) != len)
395 kprintf("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + len);
408 #endif /* CONFIG_TEST */