Move kfile interface to the io/ directory.
[bertos.git] / bertos / io / 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  * \brief Virtual KFile I/O interface.
33  *
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 #include "kfile.h"
42
43 #include "cfg/cfg_kfile.h"
44 #include <cfg/debug.h>
45 #include <cfg/log.h>
46
47 #include <drv/timer.h>
48 #include <mware/formatwr.h>
49
50 #include <string.h>
51
52 /*
53  * Sanity check for config parameters required by this module.
54  */
55 #if !defined(CONFIG_KFILE_GETS) || ((CONFIG_KFILE_GETS != 0) && CONFIG_KFILE_GETS != 1)
56         #error CONFIG_KFILE_GETS must be set to either 0 or 1 in appconfig.h
57 #endif
58 #if !defined(CONFIG_PRINTF)
59         #error CONFIG_PRINTF missing in appconfig.h
60 #endif
61
62
63 /**
64  * Generic putc() implementation using \a fd->write.
65  */
66 int kfile_putc(int _c, struct KFile *fd)
67 {
68         unsigned char c = (unsigned char)_c;
69
70         if (kfile_write(fd, &c, sizeof(c)) == sizeof(c))
71                 return (int)((unsigned char)_c);
72         else
73                 return EOF;
74 }
75
76 /**
77  * Generic getc() implementation using \a fd->read.
78  */
79 int kfile_getc(struct KFile *fd)
80 {
81         unsigned char c;
82
83         if (kfile_read(fd, &c, sizeof(c)) == sizeof(c))
84                 return (int)((unsigned char)c);
85         else
86                 return EOF;
87 }
88
89 #if CONFIG_PRINTF
90 /**
91  * Formatted write.
92  */
93 int kfile_printf(struct KFile *fd, const char *format, ...)
94 {
95         va_list ap;
96         int len;
97
98         va_start(ap, format);
99         len = _formatted_write(format, (void (*)(char, void *))kfile_putc, fd, ap);
100         va_end(ap);
101
102         return len;
103 }
104 #endif /* CONFIG_PRINTF */
105
106 /**
107  * Write a string to kfile \a fd.
108  * \return 0 if OK, EOF in case of error.
109  */
110 int kfile_print(struct KFile *fd, const char *s)
111 {
112         while (*s)
113         {
114                 if (kfile_putc(*s++, fd) == EOF)
115                         return EOF;
116         }
117         return 0;
118 }
119
120 #if CONFIG_KFILE_GETS
121 /**
122  * Read a line long at most as size and put it
123  * in buf.
124  * \return number of chars read or EOF in case
125  *         of error.
126  */
127 int kfile_gets(struct KFile *fd, char *buf, int size)
128 {
129         return kfile_gets_echo(fd, buf, size, false);
130 }
131
132
133 /**
134  * Read a line long at most as size and put it
135  * in buf, with optional echo.
136  *
137  * \return number of chars read, or EOF in case
138  *         of error.
139  */
140 int kfile_gets_echo(struct KFile *fd, char *buf, int size, bool echo)
141 {
142         int i = 0;
143         int c;
144
145         for (;;)
146         {
147                 if ((c = kfile_getc(fd)) == EOF)
148                 {
149                         buf[i] = '\0';
150                         return -1;
151                 }
152
153                 /* FIXME */
154                 if (c == '\r' || c == '\n' || i >= size-1)
155                 {
156                         buf[i] = '\0';
157                         if (echo)
158                                 kfile_print(fd, "\r\n");
159                         break;
160                 }
161                 buf[i++] = c;
162                 if (echo)
163                         kfile_putc(c, fd);
164         }
165
166         return i;
167 }
168 #endif /* !CONFIG_KFILE_GETS */
169
170
171 /**
172  * Move \a fd file seek position of \a offset bytes from \a whence.
173  *
174  * This is a generic implementation of seek function, you can redefine
175  * it in your local module if needed.
176  */
177 kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode whence)
178 {
179         kfile_off_t seek_pos;
180
181         switch (whence)
182         {
183
184         case KSM_SEEK_SET:
185                 seek_pos = 0;
186                 break;
187         case KSM_SEEK_END:
188                 seek_pos = fd->size;
189                 break;
190         case KSM_SEEK_CUR:
191                 seek_pos = fd->seek_pos;
192                 break;
193         default:
194                 ASSERT(0);
195                 return EOF;
196                 break;
197         }
198
199         #if LOG_LEVEL >= LOG_LVL_INFO
200         /* Bound check */
201         if (seek_pos + offset > fd->size)
202                 LOG_INFO("seek outside EOF\n");
203         #endif
204
205         fd->seek_pos = seek_pos + offset;
206
207         return fd->seek_pos;
208 }
209
210 /**
211  * Reopen file \a fd.
212  * This is a generic implementation that only flush file
213  * and reset seek_pos to 0.
214  */
215 struct KFile * kfile_genericReopen(struct KFile *fd)
216 {
217         kfile_flush(fd);
218         kfile_seek(fd, 0, KSM_SEEK_SET);
219         return fd;
220 }
221
222 /**
223  * Close file \a fd.
224  * This is a generic implementation that only return 0.
225  */
226 int kfile_genericClose(UNUSED_ARG(struct KFile *, fd))
227 {
228         return 0;
229 };
230
231
232 /**
233  * Discard input to resynchronize with remote end.
234  *
235  * Discard incoming data until the kfile_getc stops receiving
236  * characters for at least \a delay milliseconds.
237  *
238  * \note If the timeout occur, we reset the error before to
239  * quit.
240  */
241 void kfile_resync(KFile *fd, mtime_t delay)
242 {
243         ticks_t start_time = timer_clock();
244         for(;;)
245         {
246                 if(kfile_getc(fd) != EOF)
247                         start_time = timer_clock();
248
249                 if ((timer_clock() - start_time) > ms_to_ticks(delay))
250                 {
251                         kfile_clearerr(fd);
252                         break;
253                 }
254
255         }
256 }
257
258 /**
259  * Stub function that does nothing.
260  * This is a generic implementation that only return 0.
261  */
262 static int kfile_generic(UNUSED_ARG(struct KFile *, fd))
263 {
264         return 0;
265 };
266
267
268 /**
269  * Base class KFile constructor.
270  */
271 void kfile_init(struct KFile *fd)
272 {
273         ASSERT(fd);
274         memset(fd, 0, sizeof(*fd));
275         fd->clearerr = (ClearErrFunc_t)kfile_generic;
276         fd->close =  kfile_genericClose;
277         fd->error = kfile_generic;
278         fd->flush = kfile_generic;
279         fd->read = (ReadFunc_t)kfile_generic;
280         fd->reopen = kfile_genericReopen;
281         fd->seek = kfile_genericSeek;
282         fd->write = (WriteFunc_t)kfile_generic;
283 }
284