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/)
32 * \brief Virtual KFile I/O interface.
34 * This module implements some generic I/O interfaces for kfile.
36 * \author Francesco Sacchi <batt@develer.com>
37 * \author Daniele Basile <asterix@develer.com>
42 #include "cfg/cfg_kfile.h"
43 #include <cfg/debug.h>
46 #include <drv/timer.h>
47 #include <mware/formatwr.h>
52 * Sanity check for config parameters required by this module.
54 #if !defined(CONFIG_KFILE_GETS) || ((CONFIG_KFILE_GETS != 0) && CONFIG_KFILE_GETS != 1)
55 #error CONFIG_KFILE_GETS must be set to either 0 or 1 in appconfig.h
57 #if !defined(CONFIG_PRINTF)
58 #error CONFIG_PRINTF missing in appconfig.h
63 * Generic putc() implementation using \a fd->write.
65 int kfile_putc(int _c, struct KFile *fd)
67 unsigned char c = (unsigned char)_c;
69 if (kfile_write(fd, &c, sizeof(c)) == sizeof(c))
70 return (int)((unsigned char)_c);
76 * Generic getc() implementation using \a fd->read.
78 int kfile_getc(struct KFile *fd)
82 if (kfile_read(fd, &c, sizeof(c)) == sizeof(c))
83 return (int)((unsigned char)c);
92 int kfile_printf(struct KFile *fd, const char *format, ...)
98 len = _formatted_write(format, (void (*)(char, void *))kfile_putc, fd, ap);
103 #endif /* CONFIG_PRINTF */
106 * Write a string to kfile \a fd.
107 * \return 0 if OK, EOF in case of error.
109 int kfile_print(struct KFile *fd, const char *s)
113 if (kfile_putc(*s++, fd) == EOF)
119 #if CONFIG_KFILE_GETS
121 * Read a line long at most as size and put it
123 * \return number of chars read or EOF in case
126 int kfile_gets(struct KFile *fd, char *buf, int size)
128 return kfile_gets_echo(fd, buf, size, false);
133 * Read a line long at most as size and put it
134 * in buf, with optional echo.
136 * \return number of chars read, or EOF in case
139 int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo)
146 if ((c = kfile_getc(fd)) == EOF)
153 if (c == '\r' || c == '\n' || i >= size-1)
157 kfile_print(fd, "\r\n");
167 #endif /* !CONFIG_KFILE_GETS */
170 kfile_off_t kfile_copy(KFile *src, KFile *dst, kfile_off_t size)
173 kfile_off_t cp_len = 0;
177 size_t len = MIN(sizeof(buf), (size_t)size);
178 if (kfile_read(src, buf, len) != len)
181 size_t wr_len = kfile_write(dst, buf, len);
194 * Move \a fd file seek position of \a offset bytes from \a whence.
196 * This is a generic implementation of seek function, you can redefine
197 * it in your local module if needed.
199 kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence)
201 kfile_off_t seek_pos;
213 seek_pos = fd->seek_pos;
222 if (seek_pos + offset > fd->size)
223 LOG_INFO("seek outside EOF\n");
225 fd->seek_pos = seek_pos + offset;
232 * This is a generic implementation that only flush file
233 * and reset seek_pos to 0.
235 struct KFile * kfile_genericReopen(struct KFile *fd)
238 kfile_seek(fd, 0, KSM_SEEK_SET);
244 * This is a generic implementation that only flush the file.
246 int kfile_genericClose(struct KFile *fd)
248 return kfile_flush(fd);
253 * Discard input to resynchronize with remote end.
255 * Discard incoming data until the kfile_getc stops receiving
256 * characters for at least \a delay milliseconds.
258 * \note If the timeout occur, we reset the error before to
261 void kfile_resync(KFile *fd, mtime_t delay)
263 ticks_t start_time = timer_clock();
266 if(kfile_getc(fd) != EOF)
267 start_time = timer_clock();
269 if ((timer_clock() - start_time) > ms_to_ticks(delay))
279 * Stub function that does nothing.
280 * This is a generic implementation that only return 0.
282 static int kfile_generic(UNUSED_ARG(struct KFile *, fd))
289 * Base class KFile constructor.
291 void kfile_init(struct KFile *fd)
294 memset(fd, 0, sizeof(*fd));
295 fd->clearerr = (ClearErrFunc_t)kfile_generic;
296 fd->close = kfile_genericClose;
297 fd->error = kfile_generic;
298 fd->flush = kfile_generic;
299 fd->read = (ReadFunc_t)kfile_generic;
300 fd->reopen = kfile_genericReopen;
301 fd->seek = kfile_genericSeek;
302 fd->write = (WriteFunc_t)kfile_generic;